Exemple #1
0
        private void SaveWorld(World world)
        {
            string[] paths     = GetWorldPaths();
            string   worldName = Path.GetFileName(paths[0]);
            string   root      = GetWorldsRoot() + "/" + worldName + "/";

            world.NullifyEmpties();
            File.WriteAllText(root + "World.json", SDKUtil.SerializeObject(world));
        }
Exemple #2
0
        private static bool UpdateWorldData(string worldName)
        {
            string root  = GetWorldsRoot() + "/" + worldName + "/";
            World  world = SDKUtil.ReadJSONFileDirect <World>(root + "World.json");

            if (world == null)
            {
                EditorUtility.DisplayDialog("Error", $"World.json file for {worldName} seems corrupted and needs to be fixed first.", "OK");
                return(false);
            }

            string worldBasePath = Path.GetDirectoryName(AssetDatabase.GUIDToAssetPath(AssetDatabase.AssetPathToGUID(root + "World.json"))) + "/Pieces";

            world.objectSpecs = new List <ObjectSpec>();
            string[] assets = AssetDatabase.FindAssets("*", new[] { root + "Pieces" });
            foreach (string asset in assets)
            {
                string assetPath = AssetDatabase.GUIDToAssetPath(asset);
                if (!assetPath.ToLower().EndsWith(".prefab"))
                {
                    continue;
                }

                GameObject prefab = PrefabUtility.LoadPrefabContents(assetPath);
                if (prefab.TryGetComponent(out ExtendedAttributes ea))
                {
                    if (!ea.spec.IsDefault())
                    {
                        string prefix = Path.GetDirectoryName(assetPath);
                        if (prefix != null && prefix.Length > worldBasePath.Length)
                        {
                            prefix = prefix.Substring(worldBasePath.Length + 1) + "/";
                            prefix = prefix.Replace('\\', '/');
                        }
                        else
                        {
                            prefix = "";
                        }

                        string fileName = Path.GetFileNameWithoutExtension(assetPath);
                        ea.spec.objectKey = prefix + fileName;
                        world.objectSpecs.Add(ea.spec);
                    }
                }
                if (prefab != null)
                {
                    PrefabUtility.UnloadPrefabContents(prefab);
                }
            }

            // increase version
            if (string.IsNullOrEmpty(world.version))
            {
                world.version = "0.0.1";
            }
            else
            {
                if (Version.TryParse(world.version, out Version version))
                {
                    world.version = $"{version.Major}.{version.Minor}.{version.Build + 1}";
                }
                else
                {
                    world.version = "0.0.1";
                    Debug.LogError($"World.json of {worldName} contained an unreadable version. Resetting to {world.version}.");
                }
            }
            world.versionCode++;

            // write back
            world.NullifyEmpties();
            File.WriteAllText(root + "World.json", SDKUtil.SerializeObject(world));

            return(true);
        }