Esempio n. 1
0
    public void Write(Transform cameraPivot, VoxelArray voxelArray)
    {
        if (voxelArray.IsEmpty())
        {
            Debug.Log("World is empty! File will not be written.");
            return;
        }

        JSONObject root = new JSONObject();

        root["writerVersion"]    = VERSION;
        root["minReaderVersion"] = FILE_MIN_READER_VERSION;

        JSONObject camera = new JSONObject();

        camera["pan"]    = WriteVector3(cameraPivot.position);
        camera["rotate"] = WriteQuaternion(cameraPivot.rotation);
        camera["scale"]  = cameraPivot.localScale.z;
        root["camera"]   = camera;

        root["world"] = WriteWorld(voxelArray);

        string filePath = WorldFiles.GetFilePath(fileName);

        using (FileStream fileStream = File.Create(filePath))
        {
            using (var sw = new StreamWriter(fileStream))
            {
                sw.Write(root.ToString());
                sw.Flush();
            }
        }
    }
Esempio n. 2
0
    public static void ListWorlds(List <string> worldPaths, List <string> worldNames)
    {
        string[] files = Directory.GetFiles(WorldFiles.GetWorldsDirectory());
        worldPaths.Clear();
        foreach (string path in files)
        {
            if (WorldFiles.IsWorldFile(path))
            {
                worldPaths.Add(path);
            }
            else if (WorldFiles.IsOldWorldFile(path))
            {
                string newPath = WorldFiles.GetNewWorldPath(Path.GetFileNameWithoutExtension(path));
                Debug.Log("Updating " + path + " to " + newPath);
                File.Move(path, newPath);
                worldPaths.Add(newPath);
            }
        }
        worldPaths.Sort();

        worldNames.Clear();
        foreach (string path in worldPaths)
        {
            worldNames.Add(Path.GetFileNameWithoutExtension(path));
        }
    }
Esempio n. 3
0
    private void ImportWorld(string name)
    {
        if (name.Length == 0)
        {
            Destroy(this);
            return;
        }
        string newPath = WorldFiles.GetNewWorldPath(name);

        if (File.Exists(newPath))
        {
            var dialog = DialogGUI.ShowMessageDialog(gameObject, "A world with that name already exists.");
            dialog.yesButtonHandler = DestroyThis;
            return;
        }

        try
        {
            ShareMap.ImportSharedFile(newPath);
            MenuGUI.OpenWorld(newPath, Scenes.EDITOR);
            openingWorld = true;
            Destroy(this);
        }
        catch (System.Exception e)
        {
            Debug.Log(e);
            var dialog = DialogGUI.ShowMessageDialog(gameObject, "Error importing world");
            dialog.yesButtonHandler = DestroyThis;
        }
    }
Esempio n. 4
0
 private void OpenDemoWorld(string mapName, string templateName)
 {
     if (voxelArray == null || SelectedMap.Instance().mapName != mapName)
     {
         // create and load the file
         string filePath = WorldFiles.GetFilePath(mapName);
         if (!File.Exists(filePath))
         {
             TextAsset mapText = Resources.Load <TextAsset>(templateName);
             using (FileStream fileStream = File.Create(filePath))
             {
                 using (var sw = new StreamWriter(fileStream))
                 {
                     sw.Write(mapText.text);
                     sw.Flush();
                 }
             }
         }
         if (voxelArray != null)
         {
             voxelArray.GetComponent <EditorFile>().Save();
         }
         SelectedMap.Instance().mapName = mapName;
         SceneManager.LoadScene("editScene");
     }
     Destroy(this);
 }
Esempio n. 5
0
    private void ImportMap(string name)
    {
        if (name.Length == 0)
        {
            Close();
            return;
        }
        string newPath = WorldFiles.GetFilePath(name);

        if (File.Exists(newPath))
        {
            var dialog = DialogGUI.ShowMessageDialog(gameObject, "A world with that name already exists.");
            dialog.yesButtonHandler = Close;
            return;
        }

        try
        {
            using (FileStream fileStream = File.Create(newPath))
            {
                ShareMap.ReadSharedURLAndroid(fileStream);
            }
            Close();
        }
        catch (System.Exception e)
        {
            Debug.Log(e);
            var dialog = DialogGUI.ShowMessageDialog(gameObject, "Error importing world");
            dialog.yesButtonHandler = Close;
        }
    }
Esempio n. 6
0
    public static bool ValidateName(string name, out string errorMessage)
    {
        errorMessage = null;
        if (name.Length == 0)
        {
            return(false);
        }
        if (name.IndexOfAny(Path.GetInvalidFileNameChars()) != -1)
        {
            errorMessage = "That name contains a special character which is not allowed.";
            return(false);
        }

        if (name.StartsWith("."))
        {
            errorMessage = "Name can't start with a period.";
            return(false);
        }

        string path = WorldFiles.GetNewWorldPath(name);

        if (File.Exists(path))
        {
            errorMessage = "A world with that name already exists.";
            return(false);
        }
        return(true);  // you are valid <3
    }
Esempio n. 7
0
 private void UpdateMapList()
 {
     string[] files = Directory.GetFiles(WorldFiles.GetDirectoryPath());
     mapFiles.Clear();
     foreach (string name in files)
     {
         mapFiles.Add(Path.GetFileNameWithoutExtension(name));
     }
     mapFiles.Sort();
 }
Esempio n. 8
0
    private void CopyWorld(string newName)
    {
        if (newName.Length == 0)
        {
            return;
        }
        string newPath = WorldFiles.GetNewWorldPath(newName);

        if (File.Exists(newPath))
        {
            DialogGUI.ShowMessageDialog(gameObject, "A world with that name already exists.");
            return;
        }
        File.Copy(selectedWorldPath, newPath);
        UpdateWorldList();
    }
Esempio n. 9
0
    private void RenameMap(string newName)
    {
        if (newName.Length == 0)
        {
            return;
        }
        string newPath = WorldFiles.GetFilePath(newName);

        if (File.Exists(newPath))
        {
            DialogGUI.ShowMessageDialog(gameObject, "A world with that name already exists.");
            return;
        }
        File.Move(WorldFiles.GetFilePath(selectedWorld), newPath);
        UpdateMapList();
    }
Esempio n. 10
0
    public static void OpenDemoWorld(string name, string templateName)
    {
        if (EditorFile.instance != null)
        {
            EditorFile.instance.Save();
        }

        TextAsset worldAsset = Resources.Load <TextAsset>(templateName);
        string    path       = WorldFiles.GetNewWorldPath(name);

        for (int i = 2; File.Exists(path); i++) // autonumber
        {
            path = WorldFiles.GetNewWorldPath(name + " " + i);
        }
        SelectedWorld.SelectDemoWorld(worldAsset, path);
        SceneManager.LoadScene(Scenes.EDITOR);
    }
Esempio n. 11
0
    private void NewWorld(string name, TextAsset template)
    {
        if (name.Length == 0)
        {
            return;
        }
        string path = WorldFiles.GetNewWorldPath(name);

        if (File.Exists(path))
        {
            DialogGUI.ShowMessageDialog(gameObject, "A world with that name already exists.");
            return;
        }
        using (FileStream fileStream = File.Create(path))
        {
            fileStream.Write(template.bytes, 0, template.bytes.Length);
        }
        UpdateWorldList();

        OpenWorld(path, Scenes.EDITOR);
    }
Esempio n. 12
0
    private void CreateWorldOverflowMenu(string fileName)
    {
        worldOverflowMenu       = gameObject.AddComponent <OverflowMenuGUI>();
        selectedWorld           = fileName;
        worldOverflowMenu.items = new OverflowMenuGUI.MenuItem[]
        {
            new OverflowMenuGUI.MenuItem("Play", GUIIconSet.instance.play, () => {
                MenuGUI.OpenMap(fileName, "playScene");
            }),
            new OverflowMenuGUI.MenuItem("Rename", GUIIconSet.instance.rename, () => {
                TextInputDialogGUI inputDialog = gameObject.AddComponent <TextInputDialogGUI>();
                inputDialog.prompt             = "Enter new name for " + fileName;
                inputDialog.handler            = RenameMap;
            }),
            new OverflowMenuGUI.MenuItem("Copy", GUIIconSet.instance.copy, () => {
                TextInputDialogGUI inputDialog = gameObject.AddComponent <TextInputDialogGUI>();
                inputDialog.prompt             = "Enter new world name...";
                inputDialog.handler            = CopyMap;
            }),
            new OverflowMenuGUI.MenuItem("Delete", GUIIconSet.instance.delete, () => {
                DialogGUI dialog        = gameObject.AddComponent <DialogGUI>();
                dialog.message          = "Are you sure you want to delete " + fileName + "?";
                dialog.yesButtonText    = "Yes";
                dialog.noButtonText     = "No";
                dialog.yesButtonHandler = () =>
                {
                    File.Delete(WorldFiles.GetFilePath(fileName));
                    UpdateMapList();
                };
            }),
#if UNITY_ANDROID
            new OverflowMenuGUI.MenuItem("Share", GUIIconSet.instance.share, () => {
                string path = WorldFiles.GetFilePath(fileName);
                ShareMap.ShareAndroid(path);
            })
#endif
        };
    }
Esempio n. 13
0
    private void NewMap(string name)
    {
        if (name.Length == 0)
        {
            return;
        }
        string filePath = WorldFiles.GetFilePath(name);

        if (File.Exists(filePath))
        {
            DialogGUI.ShowMessageDialog(gameObject, "A world with that name already exists.");
            return;
        }
        using (FileStream fileStream = File.Create(filePath))
        {
            using (var sw = new StreamWriter(fileStream))
            {
                sw.Write(defaultMap.text);
                sw.Flush();
            }
        }
        UpdateMapList();
    }
Esempio n. 14
0
 void Start()
 {
     WorldFiles.ListWorlds(worldPaths, worldNames);
     EditorFile.instance.importWorldHandler = ImportWorldHandler;
 }
Esempio n. 15
0
    // return warnings
    public List <string> Read(Transform cameraPivot, VoxelArray voxelArray, bool editor)
    {
        this.editor = editor;
        if (missingMaterial == null)
        {
            // allowTransparency is true in case the material is used for an overlay, so the alpha value can be adjusted
            missingMaterial       = ResourcesDirectory.MakeCustomMaterial(ColorMode.UNLIT, true);
            missingMaterial.color = Color.magenta;
        }
        string jsonString;

        try
        {
            string filePath = WorldFiles.GetFilePath(fileName);
            using (FileStream fileStream = File.Open(filePath, FileMode.Open))
            {
                using (var sr = new StreamReader(fileStream))
                {
                    jsonString = sr.ReadToEnd();
                }
            }
        }
        catch (Exception e)
        {
            throw new MapReadException("An error occurred while reading the file", e);
        }

        JSONNode rootNode;

        try
        {
            rootNode = JSON.Parse(jsonString);
        }
        catch (Exception e)
        {
            throw new MapReadException("Invalid world file", e);
        }
        if (rootNode == null)
        {
            throw new MapReadException("Invalid world file");
        }
        JSONObject root = rootNode.AsObject;

        if (root == null || root["writerVersion"] == null || root["minReaderVersion"] == null)
        {
            throw new MapReadException("Invalid world file");
        }
        if (root["minReaderVersion"].AsInt > VERSION)
        {
            throw new MapReadException("This world file requires a newer version of the app");
        }
        fileWriterVersion = root["writerVersion"].AsInt;

        EntityReference.ResetEntityIds();

        try
        {
            if (editor && cameraPivot != null && root["camera"] != null)
            {
                ReadCamera(root["camera"].AsObject, cameraPivot);
            }
            if (root["world"] != null)
            {
                ReadWorld(root["world"].AsObject, voxelArray);
            }
        }
        catch (MapReadException e)
        {
            throw e;
        }
        catch (Exception e)
        {
            throw new MapReadException("Error reading world file", e);
        }

        EntityReference.DoneLoadingEntities();
        return(warnings);
    }
Esempio n. 16
0
 private void UpdateWorldList()
 {
     WorldFiles.ListWorlds(worldPaths, worldNames);
 }