Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Unity3d 统一保存解析问题_Unity3d - Fatal编程技术网

Unity3d 统一保存解析问题

Unity3d 统一保存解析问题,unity3d,Unity3d,我一直在试图找出这个错误,当我试图从一个简单的.gdsave(game dev save,我制作的自定义文件,与.txt没有区别)接收一个值时,我一直得到这个错误: FormatException:长度无效。 System.Convert.FromBase64String(System.String s)(at/Users/builduser/buildslave/mono/build/mcs/class/corlib/System/Convert.cs:146) Assets.USecurit

我一直在试图找出这个错误,当我试图从一个简单的.gdsave(game dev save,我制作的自定义文件,与.txt没有区别)接收一个值时,我一直得到这个错误:

FormatException:长度无效。 System.Convert.FromBase64String(System.String s)(at/Users/builduser/buildslave/mono/build/mcs/class/corlib/System/Convert.cs:146) Assets.USecurity.AES.Decrypt(System.String值,System.String密码)(位于Assets/USecurity/AES.cs:60) AESCrypt.EncryptDecrypt.aesdecypt(System.String文件路径,System.String密码)(位于Assets/Scripts/EncryptDecrypt.cs:38) SaveSys.Display.parseData(Int32 indexGrab)(位于Assets/Scripts/UI/Display.cs:32) TextToDisplay.Update()(位于Assets/Scripts/UI/TextToDisplay.cs:9)

我会把我的脚本放在下面,任何帮助都是非常感谢的,正如我所说的,很遗憾我找不到答案

Display.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using System.Text;
using Assets.USecurity;
using AESCrypt;
using TMPro;

namespace SaveSys
{
    public class Display : MonoBehaviour {

        public static string parseData(int indexGrab) // Parse the info type to return
        {

            if (1 == 2) // If we are inside the editor, we'll cheat :D (STACK OVERFLOW IGNORE THIS, I KNOW)
            {
                return "editor";
            } else
            {
                string defaultPath = Application.dataPath; // Data storage

                if (Directory.Exists(defaultPath + "/saves")) // If the save folder exists
                {
                    string currentPath = Application.dataPath + "/saves"; // Grabbing the saves folder

                    if (File.Exists(currentPath + "/" + PlayerPrefs.GetString("CurrentSaveName", "save1.gdsav"))) // Checking if there is a save file for the current game
                    {
                        string gdsavePath = currentPath + "/" + PlayerPrefs.GetString("CurrentSaveName", "save1.gdsav"); // Grabbing the current save name

                        string[] arr = AESCrypt.EncryptDecrypt.AESDecrypt(Application.dataPath + "/saves/" + PlayerPrefs.GetString("CurrentSaveName", "save1.gdsav"), "default").Split(); // Decrypt and split data
                        return arr[indexGrab].ToString(); // Return the requested value
                    } else
                    {
                        File.WriteAllText(Application.dataPath + "/saves/" + PlayerPrefs.GetString("CurrentSaveName", "save1.gdsav"), ""); // Create save file
                        return "FileCreated";
                    }
                } else
                {
                    Directory.CreateDirectory(Application.dataPath + "/saves"); // Creates save directory
                    File.WriteAllText(Application.dataPath + "/saves/" + PlayerPrefs.GetString("CurrentSaveName", "save1.gdsav"), ""); // Create save file, as it could not exist

                    string newSave = Application.dataPath + "/saves/" + PlayerPrefs.GetString("CurrentSaveName", "save1.gdsav"); // Get the save file

                    AESCrypt.EncryptDecrypt.AESEncrypt(newSave, "Default"); // Encrypt the file
                    return "FileCreated+Dir";
                }
            }
        }

        public static string changeData(string changingData)
        {
            string[] args = changingData.Split(); // Splits up the input given by UI, other scripts etc, so "0 CompanyName" becomes "0", "CompanyName"
            int index = int.Parse(args[0]); // The data to change has an args position of 0, therefor index = 0, to detect the type of data we're modifying

            // To see more about above, goto https://docs.google.com/document/d/167kAoNZddcd6k36auk7_unCegCGl4sgJ8ojjddoAmx0/edit?usp=sharing

            //foreach(string x in args) - debugging
            //{
                //print(x);
            //}
            string[] arr = File.ReadAllLines(Application.dataPath + "/saves/" + PlayerPrefs.GetString("CurrentSaveName", "save1.gdsav")); //AESCrypt.EncryptDecrypt.AESDecrypt(Application.dataPath + "/saves/" + PlayerPrefs.GetString("CurrentSaveName", "save1.gdsav"), "default").Split(); // Decrypt and split data
            if (arr.Length > int.Parse(args[0])) // If the array isn't out of range
            {

                print(index);
                print(arr.Length);
                print(args.Length);

                arr[index] = args[1].ToString(); // Changing the data's value to the second string in the array
                File.WriteAllLines(Application.dataPath + "/saves/" + PlayerPrefs.GetString("CurrentSaveName", "save1.gdsav"), arr); // Write modified data to the file
                AESCrypt.EncryptDecrypt.AESEncrypt(Application.dataPath + "/saves/" + PlayerPrefs.GetString("CurrentSaveName", "save1.gdsav"), "default"); // Encrypt the file

                return "Complete";
            }

            return "Failed";

        }   
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
using SaveSys;
public class TextToDisplay : MonoBehaviour {
    public int inde;
    void Update () {
        print(SaveSys.Display.parseData(inde));
        this.gameObject.GetComponent<TextMeshProUGUI>().text = SaveSys.Display.parseData(inde);
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using System.Text;
using Assets.USecurity;

namespace AESCrypt {
    public class EncryptDecrypt : MonoBehaviour {

        public static string AESEncrypt(string filepath, string password) // Encryption method
            {
                if (password == "default") // Keyword for default password
                {
                    password = "gdevaesencrypt"; // Default password if not specified
                    string fileText = File.ReadAllText(filepath); // Grabbing all lines to convert them
                    string encrypted = AES.Encrypt(fileText, password); // Setting encrypted to the encrypted string

                    File.WriteAllText(filepath, encrypted); // Write the encrypted value to the file
                    return encrypted;
                } else
                {
                    string fileText = File.ReadAllText(filepath); // Grabbing all lines to convert them
                    string encrypted = AES.Encrypt(fileText, password); // Setting encrypted to the encrypted string

                    File.WriteAllText(filepath, encrypted); // Write the encrypted value to the file
                    return encrypted;
                }

            }

            public static string AESDecrypt(string filepath, string password) // Encryption method
            {
                if (password == "default") // Keyword for default password
                {
                    password = "gdevaesencrypt"; // Default password if not specified
                    string fileText = File.ReadAllText(filepath); // Grabbing all lines to convert them
                    string decrypted = AES.Decrypt(fileText, password); // Setting decrypted to the encrypted string

                    return decrypted;
                } else
                {
                    string fileText = File.ReadAllText(filepath); // Grabbing all lines to convert them
                    string decrypted = AES.Decrypt(fileText, password); // Setting decrypted to the encrypted string

                    return decrypted;
                }
            }
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using SaveSys;
using TMPro;
public class UIElements : MonoBehaviour {
    public int index;

    public void nameThing(TMP_InputField inp)
    {
        SaveSys.Display.changeData(inp.GetComponent<UIElements>().index + " " + inp.text);
    }
}
TextToDisplay.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using System.Text;
using Assets.USecurity;
using AESCrypt;
using TMPro;

namespace SaveSys
{
    public class Display : MonoBehaviour {

        public static string parseData(int indexGrab) // Parse the info type to return
        {

            if (1 == 2) // If we are inside the editor, we'll cheat :D (STACK OVERFLOW IGNORE THIS, I KNOW)
            {
                return "editor";
            } else
            {
                string defaultPath = Application.dataPath; // Data storage

                if (Directory.Exists(defaultPath + "/saves")) // If the save folder exists
                {
                    string currentPath = Application.dataPath + "/saves"; // Grabbing the saves folder

                    if (File.Exists(currentPath + "/" + PlayerPrefs.GetString("CurrentSaveName", "save1.gdsav"))) // Checking if there is a save file for the current game
                    {
                        string gdsavePath = currentPath + "/" + PlayerPrefs.GetString("CurrentSaveName", "save1.gdsav"); // Grabbing the current save name

                        string[] arr = AESCrypt.EncryptDecrypt.AESDecrypt(Application.dataPath + "/saves/" + PlayerPrefs.GetString("CurrentSaveName", "save1.gdsav"), "default").Split(); // Decrypt and split data
                        return arr[indexGrab].ToString(); // Return the requested value
                    } else
                    {
                        File.WriteAllText(Application.dataPath + "/saves/" + PlayerPrefs.GetString("CurrentSaveName", "save1.gdsav"), ""); // Create save file
                        return "FileCreated";
                    }
                } else
                {
                    Directory.CreateDirectory(Application.dataPath + "/saves"); // Creates save directory
                    File.WriteAllText(Application.dataPath + "/saves/" + PlayerPrefs.GetString("CurrentSaveName", "save1.gdsav"), ""); // Create save file, as it could not exist

                    string newSave = Application.dataPath + "/saves/" + PlayerPrefs.GetString("CurrentSaveName", "save1.gdsav"); // Get the save file

                    AESCrypt.EncryptDecrypt.AESEncrypt(newSave, "Default"); // Encrypt the file
                    return "FileCreated+Dir";
                }
            }
        }

        public static string changeData(string changingData)
        {
            string[] args = changingData.Split(); // Splits up the input given by UI, other scripts etc, so "0 CompanyName" becomes "0", "CompanyName"
            int index = int.Parse(args[0]); // The data to change has an args position of 0, therefor index = 0, to detect the type of data we're modifying

            // To see more about above, goto https://docs.google.com/document/d/167kAoNZddcd6k36auk7_unCegCGl4sgJ8ojjddoAmx0/edit?usp=sharing

            //foreach(string x in args) - debugging
            //{
                //print(x);
            //}
            string[] arr = File.ReadAllLines(Application.dataPath + "/saves/" + PlayerPrefs.GetString("CurrentSaveName", "save1.gdsav")); //AESCrypt.EncryptDecrypt.AESDecrypt(Application.dataPath + "/saves/" + PlayerPrefs.GetString("CurrentSaveName", "save1.gdsav"), "default").Split(); // Decrypt and split data
            if (arr.Length > int.Parse(args[0])) // If the array isn't out of range
            {

                print(index);
                print(arr.Length);
                print(args.Length);

                arr[index] = args[1].ToString(); // Changing the data's value to the second string in the array
                File.WriteAllLines(Application.dataPath + "/saves/" + PlayerPrefs.GetString("CurrentSaveName", "save1.gdsav"), arr); // Write modified data to the file
                AESCrypt.EncryptDecrypt.AESEncrypt(Application.dataPath + "/saves/" + PlayerPrefs.GetString("CurrentSaveName", "save1.gdsav"), "default"); // Encrypt the file

                return "Complete";
            }

            return "Failed";

        }   
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
using SaveSys;
public class TextToDisplay : MonoBehaviour {
    public int inde;
    void Update () {
        print(SaveSys.Display.parseData(inde));
        this.gameObject.GetComponent<TextMeshProUGUI>().text = SaveSys.Display.parseData(inde);
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using System.Text;
using Assets.USecurity;

namespace AESCrypt {
    public class EncryptDecrypt : MonoBehaviour {

        public static string AESEncrypt(string filepath, string password) // Encryption method
            {
                if (password == "default") // Keyword for default password
                {
                    password = "gdevaesencrypt"; // Default password if not specified
                    string fileText = File.ReadAllText(filepath); // Grabbing all lines to convert them
                    string encrypted = AES.Encrypt(fileText, password); // Setting encrypted to the encrypted string

                    File.WriteAllText(filepath, encrypted); // Write the encrypted value to the file
                    return encrypted;
                } else
                {
                    string fileText = File.ReadAllText(filepath); // Grabbing all lines to convert them
                    string encrypted = AES.Encrypt(fileText, password); // Setting encrypted to the encrypted string

                    File.WriteAllText(filepath, encrypted); // Write the encrypted value to the file
                    return encrypted;
                }

            }

            public static string AESDecrypt(string filepath, string password) // Encryption method
            {
                if (password == "default") // Keyword for default password
                {
                    password = "gdevaesencrypt"; // Default password if not specified
                    string fileText = File.ReadAllText(filepath); // Grabbing all lines to convert them
                    string decrypted = AES.Decrypt(fileText, password); // Setting decrypted to the encrypted string

                    return decrypted;
                } else
                {
                    string fileText = File.ReadAllText(filepath); // Grabbing all lines to convert them
                    string decrypted = AES.Decrypt(fileText, password); // Setting decrypted to the encrypted string

                    return decrypted;
                }
            }
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using SaveSys;
using TMPro;
public class UIElements : MonoBehaviour {
    public int index;

    public void nameThing(TMP_InputField inp)
    {
        SaveSys.Display.changeData(inp.GetComponent<UIElements>().index + " " + inp.text);
    }
}
UI元素.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using System.Text;
using Assets.USecurity;
using AESCrypt;
using TMPro;

namespace SaveSys
{
    public class Display : MonoBehaviour {

        public static string parseData(int indexGrab) // Parse the info type to return
        {

            if (1 == 2) // If we are inside the editor, we'll cheat :D (STACK OVERFLOW IGNORE THIS, I KNOW)
            {
                return "editor";
            } else
            {
                string defaultPath = Application.dataPath; // Data storage

                if (Directory.Exists(defaultPath + "/saves")) // If the save folder exists
                {
                    string currentPath = Application.dataPath + "/saves"; // Grabbing the saves folder

                    if (File.Exists(currentPath + "/" + PlayerPrefs.GetString("CurrentSaveName", "save1.gdsav"))) // Checking if there is a save file for the current game
                    {
                        string gdsavePath = currentPath + "/" + PlayerPrefs.GetString("CurrentSaveName", "save1.gdsav"); // Grabbing the current save name

                        string[] arr = AESCrypt.EncryptDecrypt.AESDecrypt(Application.dataPath + "/saves/" + PlayerPrefs.GetString("CurrentSaveName", "save1.gdsav"), "default").Split(); // Decrypt and split data
                        return arr[indexGrab].ToString(); // Return the requested value
                    } else
                    {
                        File.WriteAllText(Application.dataPath + "/saves/" + PlayerPrefs.GetString("CurrentSaveName", "save1.gdsav"), ""); // Create save file
                        return "FileCreated";
                    }
                } else
                {
                    Directory.CreateDirectory(Application.dataPath + "/saves"); // Creates save directory
                    File.WriteAllText(Application.dataPath + "/saves/" + PlayerPrefs.GetString("CurrentSaveName", "save1.gdsav"), ""); // Create save file, as it could not exist

                    string newSave = Application.dataPath + "/saves/" + PlayerPrefs.GetString("CurrentSaveName", "save1.gdsav"); // Get the save file

                    AESCrypt.EncryptDecrypt.AESEncrypt(newSave, "Default"); // Encrypt the file
                    return "FileCreated+Dir";
                }
            }
        }

        public static string changeData(string changingData)
        {
            string[] args = changingData.Split(); // Splits up the input given by UI, other scripts etc, so "0 CompanyName" becomes "0", "CompanyName"
            int index = int.Parse(args[0]); // The data to change has an args position of 0, therefor index = 0, to detect the type of data we're modifying

            // To see more about above, goto https://docs.google.com/document/d/167kAoNZddcd6k36auk7_unCegCGl4sgJ8ojjddoAmx0/edit?usp=sharing

            //foreach(string x in args) - debugging
            //{
                //print(x);
            //}
            string[] arr = File.ReadAllLines(Application.dataPath + "/saves/" + PlayerPrefs.GetString("CurrentSaveName", "save1.gdsav")); //AESCrypt.EncryptDecrypt.AESDecrypt(Application.dataPath + "/saves/" + PlayerPrefs.GetString("CurrentSaveName", "save1.gdsav"), "default").Split(); // Decrypt and split data
            if (arr.Length > int.Parse(args[0])) // If the array isn't out of range
            {

                print(index);
                print(arr.Length);
                print(args.Length);

                arr[index] = args[1].ToString(); // Changing the data's value to the second string in the array
                File.WriteAllLines(Application.dataPath + "/saves/" + PlayerPrefs.GetString("CurrentSaveName", "save1.gdsav"), arr); // Write modified data to the file
                AESCrypt.EncryptDecrypt.AESEncrypt(Application.dataPath + "/saves/" + PlayerPrefs.GetString("CurrentSaveName", "save1.gdsav"), "default"); // Encrypt the file

                return "Complete";
            }

            return "Failed";

        }   
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
using SaveSys;
public class TextToDisplay : MonoBehaviour {
    public int inde;
    void Update () {
        print(SaveSys.Display.parseData(inde));
        this.gameObject.GetComponent<TextMeshProUGUI>().text = SaveSys.Display.parseData(inde);
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using System.Text;
using Assets.USecurity;

namespace AESCrypt {
    public class EncryptDecrypt : MonoBehaviour {

        public static string AESEncrypt(string filepath, string password) // Encryption method
            {
                if (password == "default") // Keyword for default password
                {
                    password = "gdevaesencrypt"; // Default password if not specified
                    string fileText = File.ReadAllText(filepath); // Grabbing all lines to convert them
                    string encrypted = AES.Encrypt(fileText, password); // Setting encrypted to the encrypted string

                    File.WriteAllText(filepath, encrypted); // Write the encrypted value to the file
                    return encrypted;
                } else
                {
                    string fileText = File.ReadAllText(filepath); // Grabbing all lines to convert them
                    string encrypted = AES.Encrypt(fileText, password); // Setting encrypted to the encrypted string

                    File.WriteAllText(filepath, encrypted); // Write the encrypted value to the file
                    return encrypted;
                }

            }

            public static string AESDecrypt(string filepath, string password) // Encryption method
            {
                if (password == "default") // Keyword for default password
                {
                    password = "gdevaesencrypt"; // Default password if not specified
                    string fileText = File.ReadAllText(filepath); // Grabbing all lines to convert them
                    string decrypted = AES.Decrypt(fileText, password); // Setting decrypted to the encrypted string

                    return decrypted;
                } else
                {
                    string fileText = File.ReadAllText(filepath); // Grabbing all lines to convert them
                    string decrypted = AES.Decrypt(fileText, password); // Setting decrypted to the encrypted string

                    return decrypted;
                }
            }
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using SaveSys;
using TMPro;
public class UIElements : MonoBehaviour {
    public int index;

    public void nameThing(TMP_InputField inp)
    {
        SaveSys.Display.changeData(inp.GetComponent<UIElements>().index + " " + inp.text);
    }
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
使用SaveSys;
使用TMPro;
公共类元素:单一行为{
公共整数指数;
公共无效名称(TMP\U输入字段输入)
{
SaveSys.Display.changeData(inp.GetComponent().index+“”+inp.text);
}
}

在创建目录的
else
中,您正在使用错误的密码创建文件,使用大写字母D的“Default”(默认值)。这可能是导致问题的原因吗?在本部分中创建文件时,可能还应加密:

else
{
    File.WriteAllText(Application.dataPath + "/saves/" + PlayerPrefs.GetString("CurrentSaveName", "save1.gdsav"), ""); // Create save file
    return "FileCreated";
}

谢谢,但我决定重新启动我的文件系统,因为它无论如何都有点笨重,但我会将此设置为将来用户推荐的答案。