public static void SetMostRecentlyDeletedBoard(GameObject board)
    {
        if (!Directory.Exists(Application.persistentDataPath + "/backups"))
        {
            Directory.CreateDirectory(Application.persistentDataPath + "/backups");
        }
        if (!Directory.Exists(Application.persistentDataPath + "/backups/_____deletedboards"))
        {
            Directory.CreateDirectory(Application.persistentDataPath + "/backups/_____deletedboards");
        }

        // delete excess board backups
        string[] Backups = Directory.GetFiles(Application.persistentDataPath + "/backups/_____deletedboards");
        System.Array.Sort(Backups); // get it in alphabetical order, so oldest backups are at the start of the array
        int ExcessBackups = Backups.Length - MaxBoardBackups;

        for (int i = ExcessBackups - 2; i >= 0; i--)
        {
            File.Delete(Backups[i]);
        }

        SavedObjectV2 MostRecentlyDeletedBoard = SavedObjectUtilities.CreateSavedObjectFrom(board);              // only use the main thread for what's absolutely necessary

        LoadBoardMenu.Instance.StartCoroutineAsync(SaveMostRecentlyDeletedBoardAsync(MostRecentlyDeletedBoard)); // because it has to start on a monobehavior...
    }
Exemplo n.º 2
0
        public static void ToJson(string board_filename, string json_filename = null)
        {
            json_filename = json_filename ?? Path.GetFileNameWithoutExtension(board_filename) + ".json";
            if (verbose)
            {
                Console.WriteLine($"Converting from tungboard to JSON ({board_filename} -> {json_filename})");
            }
            SavedObjectV2 board = ReadBoard(board_filename);

            if (verbose)
            {
                Console.WriteLine("Board read");
            }
            string serialized = JsonConvert.SerializeObject(board,
                                                            indent ? Formatting.Indented : Formatting.None,
                                                            new JsonSerializerSettings
            {
                TypeNameHandling = TypeNameHandling.Auto
            });

            if (verbose)
            {
                Console.WriteLine("Board serialized");
            }
            File.WriteAllText(json_filename, serialized);
        }
Exemplo n.º 3
0
 public static void LoadSavedObject(SavedObjectV2 save, ref GameObject __result)
 {
     if (save.Children?.SingleOrDefault(o => o is SavedNetObject) is SavedNetObject net)
     {
         var netObj = __result.GetComponent <NetObject>() ?? __result.AddComponent <NetObject>();
         netObj.NetID = net.NetID;
     }
 }
    private static IEnumerator SaveMostRecentlyDeletedBoardAsync(SavedObjectV2 MostRecentlyDeletedBoard)
    {
        yield return(Ninja.JumpToUnity);

        string path = Application.persistentDataPath + "/backups/_____deletedboards";

        yield return(Ninja.JumpBack);

        FileUtilities.SaveToFile(path, FileUtilities.CurrentTimestamp + ".tungboard", MostRecentlyDeletedBoard);
    }
Exemplo n.º 5
0
    public void ConfirmSaveBoard()
    {
        if (BoardBeingSaved == null)
        {
            Done(); return;
        }

        SavedObjectV2 BoardSavedObject = SavedObjectUtilities.CreateSavedObjectFrom(BoardBeingSaved);

        FileUtilities.SaveToFile(Application.persistentDataPath + "/savedboards/", FileUtilities.ValidatedUniqueBoardName(BoardNameInput.text) + ".tungboard", BoardSavedObject);

        LoadBoardMenu.Instance.GenerateLoadBoardsMenu();
        Done();
    }
    public static void RestoreMostRecentlyDeletedBoard()
    {
        string[] BoardBackups = Directory.GetFiles(Application.persistentDataPath + "/backups/_____deletedboards");
        System.Array.Sort(BoardBackups);

        SavedObjectV2 save        = (SavedObjectV2)FileUtilities.LoadFromFile(BoardBackups[BoardBackups.Length - 1]);
        GameObject    LoadedBoard = SavedObjectUtilities.LoadSavedObject(save);

        LoadedBoard.transform.position = new Vector3(0, -2000, 0);
        RecalculateClustersOfBoard(LoadedBoard);

        BoardPlacer.NewBoardBeingPlaced(LoadedBoard);
        GameplayUIManager.UIState = UIState.BoardBeingPlaced;
    }
    public void Load()
    {
        if (SelectedBoard == null)
        {
            return;
        }

        SavedObjectV2 save        = (SavedObjectV2)FileUtilities.LoadFromFile(SelectedBoard.FilePath);
        GameObject    LoadedBoard = SavedObjectUtilities.LoadSavedObject(save);

        HideAll();

        LoadedBoard.transform.position = new Vector3(0, -2000, 0);
        BoardFunctions.RecalculateClustersOfBoard(LoadedBoard);

        BoardPlacer.NewBoardBeingPlaced(LoadedBoard);
        GameplayUIManager.UIState = UIState.BoardBeingPlaced;
    }
        static void Postfix(ObjectInfo worldsave, ref SavedObjectV2 __result)
        {
            var netObj = worldsave.GetComponent <NetObject>();

            if (netObj != null)
            {
                if (__result.Children == null)
                {
                    __result.Children = new SavedObjectV2[0];
                }

                SavedObjectV2[] newChildren = new SavedObjectV2[__result.Children.Length + 1];
                Array.Copy(__result.Children, 0, newChildren, 1, __result.Children.Length);

                newChildren[0] = new SavedNetObject(netObj.NetID);

                __result.Children = newChildren;
            }
        }
Exemplo n.º 9
0
        static bool Prefix(ref SavedObjectV2 __result, ObjectInfo worldsave)
        {
            SavedObjectV2 savedObjectV = null;

            if (worldsave.ComponentType == ComponentType.CustomObject)
            {
                savedObjectV = new SavedCustomObject();

                CreateCustomSavedObjectPatch.Prefix((SavedCustomObject)savedObjectV, worldsave);

                savedObjectV.LocalPosition    = worldsave.transform.localPosition;
                savedObjectV.LocalEulerAngles = worldsave.transform.localEulerAngles;
                savedObjectV.Children         = new SavedObjectV2[0];

                __result = savedObjectV;
                return(false);
            }

            return(true);
        }
Exemplo n.º 10
0
        public static void ToBoard(string json_filename, string board_filename = null)
        {
            board_filename = board_filename ?? Path.GetFileNameWithoutExtension(json_filename) + ".tungboard";
            if (verbose)
            {
                Console.WriteLine($"Converting from JSON to tungboard ({json_filename} -> {board_filename})");
            }
            SavedObjectV2 board = ReadJson(json_filename);

            if (verbose)
            {
                Console.WriteLine("Board read");
            }
            FileStream      fs        = new FileStream(board_filename, FileMode.OpenOrCreate);
            BinaryFormatter formatter = new BinaryFormatter();

            formatter.Serialize(fs, board);
            if (verbose)
            {
                Console.WriteLine("Board serialized");
            }
        }
Exemplo n.º 11
0
 static void Prefix(SavedObjectV2 save, Transform parent)
 {
     GetCustomPrefabPatch.NextParent = parent;
 }
    public static SavedObjectV2 CreateSavedObjectFrom(ObjectInfo worldsave)
    {
        //SavedObjectV2 newsavedobject = SaveManager.ObjectTypeToSavedObjectType(save.ObjectType);

        SavedObjectV2 newsavedobject = null;

        switch (worldsave.ComponentType)
        {
        case ComponentType.CustomObject:
            newsavedobject = new SavedCustomObject();
            CreateCustomSavedObject((SavedCustomObject)newsavedobject, worldsave);
            break;

        case ComponentType.CircuitBoard:
            CircuitBoard board = worldsave.GetComponent <CircuitBoard>();
            newsavedobject = new SavedCircuitBoard
            {
                x     = board.x,
                z     = board.z,
                color = board.GetBoardColor
            };
            break;

        case ComponentType.Wire:
            InputInputConnection IIConnection = worldsave.GetComponent <InputInputConnection>();
            newsavedobject = new SavedWire
            {
                InputInput = IIConnection,
                length     = worldsave.transform.localScale.z
            };
            break;

        case ComponentType.Button:
            newsavedobject = new SavedButton();
            break;

        case ComponentType.PanelButton:
            newsavedobject = new SavedPanelButton();
            break;

        case ComponentType.Delayer:
            Delayer delayer = worldsave.GetComponent <Delayer>();
            newsavedobject = new SavedDelayer
            {
                OutputOn   = delayer.Output.On,
                DelayCount = delayer.DelayCount
            };
            break;

        case ComponentType.Display:
            Display display = worldsave.GetComponentInChildren <Display>();
            newsavedobject = new SavedDisplay
            {
                Color = display.DisplayColor
            };
            break;

        case ComponentType.PanelDisplay:
            Display paneldisplay = worldsave.GetComponentInChildren <Display>();
            newsavedobject = new SavedPanelDisplay
            {
                Color = paneldisplay.DisplayColor
            };
            break;

        case ComponentType.Inverter:
            Inverter notgate = worldsave.GetComponent <Inverter>();
            newsavedobject = new SavedInverter
            {
                OutputOn = notgate.Output.On
            };
            break;

        case ComponentType.Label:
            Label label = worldsave.GetComponent <Label>();
            newsavedobject = new SavedLabel
            {
                text     = label.text.text,
                FontSize = label.text.fontSize
            };
            break;

        case ComponentType.PanelLabel:
            Label panellabel = worldsave.GetComponent <Label>();
            newsavedobject = new SavedPanelLabel
            {
                text     = panellabel.text.text,
                FontSize = panellabel.text.fontSize
            };
            break;

        case ComponentType.Switch:     // SWITCH-F*****G-CEPTION
            Switch circuitswitch = worldsave.GetComponentInChildren <Switch>();
            newsavedobject = new SavedSwitch
            {
                on = circuitswitch.On
            };
            break;

        case ComponentType.PanelSwitch:
            Switch panelswitch = worldsave.GetComponentInChildren <Switch>();
            newsavedobject = new SavedPanelSwitch
            {
                on = panelswitch.On
            };
            break;

        case ComponentType.Peg:
            newsavedobject = new SavedPeg();
            break;

        case ComponentType.ThroughPeg:
            newsavedobject = new SavedThroughPeg();
            break;

        case ComponentType.Blotter:
            Blotter blotter = worldsave.GetComponent <Blotter>();
            newsavedobject = new SavedBlotter
            {
                OutputOn = blotter.Output.On
            };
            break;

        case ComponentType.ThroughBlotter:
            Blotter throughblotter = worldsave.GetComponent <Blotter>();
            newsavedobject = new SavedThroughBlotter
            {
                OutputOn = throughblotter.Output.On
            };
            break;

        case ComponentType.ColorDisplay:
            newsavedobject = new SavedColorDisplay();
            break;

        case ComponentType.PanelColorDisplay:
            newsavedobject = new SavedPanelColorDisplay();
            break;

        case ComponentType.Noisemaker:
            Noisemaker noisemaker = worldsave.GetComponentInChildren <Noisemaker>();
            newsavedobject = new SavedNoisemaker
            {
                ToneFrequency = noisemaker.ToneFrequency
            };
            break;

        case ComponentType.SnappingPeg:
            newsavedobject = new SavedSnappingPeg();
            break;

        case ComponentType.Mount:
            newsavedobject = new SavedMount();
            break;

        case ComponentType.VerticalSnappingPeg:
            newsavedobject = new SavedVerticalSnappingPeg();
            break;

        case ComponentType.none:
            Debug.LogError("BIG ERROR tried to save a component with no type!");
            break;
        }

        newsavedobject.LocalPosition    = worldsave.transform.localPosition;
        newsavedobject.LocalEulerAngles = worldsave.transform.localEulerAngles;

        if (newsavedobject.CanHaveChildren)
        {
            newsavedobject.Children = FindChildSaves(worldsave);
        }

        return(newsavedobject);
    }
    public static GameObject LoadSavedObject(SavedObjectV2 save, Transform parent = null)
    {
        ComponentType componenttype = SavedObjectTypeToObjectType[save.GetType()];

        GameObject LoadedObject = null;

        if (componenttype == ComponentType.CustomObject)
        {
            LoadedObject = GetCustomPrefab((SavedCustomObject)save);
        }
        else
        {
            LoadedObject = UnityEngine.Object.Instantiate(Prefabs.ComponentTypeToPrefab(componenttype), parent);
        }

        LoadedObject.transform.localPosition    = save.LocalPosition;
        LoadedObject.transform.localEulerAngles = save.LocalEulerAngles;

        LoadedObject.AddComponent <ObjectInfo>().ComponentType = componenttype;

        switch (componenttype)
        {
        // there is no case for objects with no special data to load

        case ComponentType.CustomObject:
            LoadCustomObject(LoadedObject, (SavedCustomObject)save);
            break;


        case ComponentType.CircuitBoard:
            LoadCircuitBoard(LoadedObject, (SavedCircuitBoard)save);
            break;

        case ComponentType.Wire:
            LoadWire(LoadedObject, (SavedWire)save);
            break;

        case ComponentType.Delayer:
            LoadDelayer(LoadedObject, (SavedDelayer)save);
            break;

        case ComponentType.PanelDisplay:
        case ComponentType.Display:
            LoadDisplay(LoadedObject, (SavedDisplay)save);
            break;

        case ComponentType.Inverter:
            LoadInverter(LoadedObject, (SavedInverter)save);
            break;

        case ComponentType.PanelLabel:
        case ComponentType.Label:
            LoadLabel(LoadedObject, (SavedLabel)save);
            break;

        case ComponentType.PanelSwitch:
        case ComponentType.Switch:     // SWITCH-F*****G-CEPTION
            LoadSwitch(LoadedObject, (SavedSwitch)save);
            break;

        case ComponentType.ThroughBlotter:
        case ComponentType.Blotter:
            LoadBlotter(LoadedObject, (SavedBlotter)save);
            break;

        case ComponentType.Noisemaker:
            LoadNoisemaker(LoadedObject, (SavedNoisemaker)save);
            break;

        case ComponentType.Mount:
            LoadMount(LoadedObject);
            break;
        }

        if (save.CanHaveChildren)
        {
            foreach (SavedObjectV2 child in save.Children)
            {
                LoadSavedObject(child, LoadedObject.transform);
            }
        }

        return(LoadedObject);
    }
Exemplo n.º 14
0
 public static bool LoadSavedObject(SavedObjectV2 save)
 {
     return(!(save is SavedNetObject));
 }