Exemplo n.º 1
0
    public void LoadSectorFile(string path)
    {
        // Update passed path during load time for main saves updating to newer versions
        if (path.Contains("main") || path == "")
        {
            path = jsonPath;
        }

        resourcePath = path;
        if (System.IO.Directory.Exists(path))
        {
            try
            {
                // resource pack loading
                if (!ResourceManager.Instance.LoadResources(path) && testResourcePath != null)
                {
                    ResourceManager.Instance.LoadResources(testResourcePath);
                }

                foreach (var canvas in Directory.GetFiles(path + "\\Canvases"))
                {
                    if (canvas.Contains(".meta"))
                    {
                        continue;
                    }

                    if (canvas.Contains(".taskdata"))
                    {
                        taskManager.AddCanvasPath(canvas);
                        continue;
                    }

                    if (canvas.Contains(".sectordata"))
                    {
                        taskManager.AddCanvasPath(canvas);
                        continue;
                    }

                    if (canvas.Contains(".dialoguedata"))
                    {
                        dialogueSystem.AddCanvasPath(canvas);
                        continue;
                    }
                }


                // sector and world handling
                string[] files = Directory.GetFiles(path);
                current = null;
                sectors = new List <Sector>();
                minimapSectorBorders = new Dictionary <Sector, LineRenderer>();
                foreach (string file in files)
                {
                    if (file.Contains(".meta") || file.Contains("ResourceData.txt"))
                    {
                        continue;
                    }

                    // parse world data
                    if (file.Contains(".worlddata"))
                    {
                        string    worlddatajson = System.IO.File.ReadAllText(file);
                        WorldData wdata         = ScriptableObject.CreateInstance <WorldData>();
                        JsonUtility.FromJsonOverwrite(worlddatajson, wdata);
                        spawnPoint = wdata.initialSpawn;
                        if (player.cursave == null || player.cursave.timePlayed == 0)
                        {
                            player.transform.position = player.spawnPoint = player.havenSpawnPoint = spawnPoint;
                            if (wdata.defaultBlueprintJSON != null && wdata.defaultBlueprintJSON != "")
                            {
                                if (player.cursave != null)
                                {
                                    player.cursave.currentPlayerBlueprint = wdata.defaultBlueprintJSON;
                                }
                                Debug.Log("Default blueprint set");
                            }
                        }

                        if (characters == null || characters.Length == 0)
                        {
                            characters = wdata.defaultCharacters;
                        }
                        else
                        {
                            // if there were added characters into the map since the last save, they must be added
                            // into the existing data
                            List <WorldData.CharacterData> charList = new List <WorldData.CharacterData>(characters);
                            foreach (var defaultChar in wdata.defaultCharacters)
                            {
                                Debug.Log(defaultChar.ID);
                                if (!charList.TrueForAll(ch => ch.ID != defaultChar.ID))
                                {
                                    charList.Add(defaultChar);
                                }
                                else
                                {
                                    // We want to make sure names and IDs match at all times since these won't be changeable
                                    // by the player and the game needs these to match
                                    charList.Find(ch => ch.ID == defaultChar.ID).name = defaultChar.name;
                                }
                            }
                            characters = charList.ToArray();
                        }

                        PartIndexScript.index = wdata.partIndexDataArray;
                        continue;
                    }

                    if (ResourceManager.fileNames.Contains(file))
                    {
                        continue;
                    }

                    string sectorjson = System.IO.File.ReadAllText(file);
                    SectorCreatorMouse.SectorData data = JsonUtility.FromJson <SectorCreatorMouse.SectorData>(sectorjson);
                    // Debug.Log("Platform JSON: " + data.platformjson);
                    // Debug.Log("Sector JSON: " + data.sectorjson);
                    Sector curSect = ScriptableObject.CreateInstance <Sector>();
                    JsonUtility.FromJsonOverwrite(data.sectorjson, curSect);

                    if (data.platformjson != "") // If the file has old platform data
                    {
                        LandPlatform plat = ScriptableObject.CreateInstance <LandPlatform>();
                        JsonUtility.FromJsonOverwrite(data.platformjson, plat);
                        plat.name        = curSect.name + "Platform";
                        curSect.platform = plat;
                    }

                    // render the borders on the minimap
                    var border = new GameObject("MinimapSectorBorder - " + curSect.sectorName).AddComponent <LineRenderer>();
                    border.material         = ResourceManager.GetAsset <Material>("white_material");
                    border.gameObject.layer = 8;
                    border.positionCount    = 4;
                    border.startWidth       = 0.5f;
                    border.endWidth         = 0.5f;
                    border.loop             = true;
                    border.SetPositions(new Vector3[] {
                        new Vector3(curSect.bounds.x, curSect.bounds.y, 0),
                        new Vector3(curSect.bounds.x + curSect.bounds.w, curSect.bounds.y, 0),
                        new Vector3(curSect.bounds.x + curSect.bounds.w, curSect.bounds.y - curSect.bounds.h, 0),
                        new Vector3(curSect.bounds.x, curSect.bounds.y - curSect.bounds.h, 0)
                    });
                    border.enabled    = player.cursave.sectorsSeen.Contains(curSect.sectorName);
                    border.startColor = border.endColor = new Color32((byte)135, (byte)135, (byte)135, (byte)255);
                    minimapSectorBorders.Add(curSect, border);

                    sectors.Add(curSect);
                }
                player.SetIsInteracting(false);

                jsonMode     = false;
                sectorLoaded = true;
                return;
            }
            catch (System.Exception e)
            {
                Debug.LogError(e);
            };
        }
        else if (System.IO.File.Exists(path))
        {
            try
            {
                string sectorjson = System.IO.File.ReadAllText(path);
                SectorCreatorMouse.SectorData data = JsonUtility.FromJson <SectorCreatorMouse.SectorData>(sectorjson);
                Debug.Log("Platform JSON: " + data.platformjson);
                Debug.Log("Sector JSON: " + data.sectorjson);
                Sector curSect = ScriptableObject.CreateInstance <Sector>();
                JsonUtility.FromJsonOverwrite(data.sectorjson, curSect);
                if (data.platformjson != "") // If the file has old platform data
                {
                    LandPlatform plat = ScriptableObject.CreateInstance <LandPlatform>();
                    JsonUtility.FromJsonOverwrite(data.platformjson, plat);
                    plat.name        = curSect.name + "Platform";
                    curSect.platform = plat;
                }
                current = curSect;
                sectors = new List <Sector>();
                sectors.Add(curSect);
                Debug.Log("Success! File loaded from " + path);
                jsonMode     = false;
                sectorLoaded = true;
                player.SetIsInteracting(false);
                loadSector();
                return;
            }
            catch (System.Exception e)
            {
                Debug.LogError(e);
            }
        }
        Debug.LogError("Could not find valid sector in " + path);
        jsonMode = false;
        player.SetIsInteracting(false);
        loadSector();
        sectorLoaded = true;
    }