public static PlaceBoardPacket BuildFromBoard(CircuitBoard board, Transform parent) { var netObj = board.GetComponent <NetObject>(); if (netObj == null) { netObj = board.gameObject.AddComponent <NetObject>(); netObj.NetID = NetObject.GetNewID(); } var packet = new PlaceBoardPacket { AuthorID = NetworkClient.Instance.PlayerID, ParentBoardID = parent?.GetComponent <NetObject>()?.NetID ?? 0, Position = board.transform.position, EulerAngles = board.transform.eulerAngles }; var savedObj = SavedObjectUtilities.CreateSavedObjectFrom(board.gameObject); using (var mem = new MemoryStream()) { BinFormatter.Serialize(mem, savedObj); packet.SavedBoard = mem.ToArray(); } return(packet); }
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... }
public static void Deserialize(byte[] data) { //Clear scene MegaMeshManager.ClearReferences(); BehaviorManager.ClearAllLists(); foreach (ObjectInfo objectInfo in GameObject.FindObjectsOfType <ObjectInfo>()) { GameObject.Destroy(objectInfo.gameObject); } SavedWorld world; //Deserialize the data into a SavedWorld using (MemoryStream mem = new MemoryStream(Compressor.Decompress(data))) { world = (SavedWorld) new BinaryFormatter().Deserialize(mem); } //Run on Unity thread MulTUNG.SynchronizationContext.Send(_ => { //Load all the objects from the save foreach (var item in world.TopLevelObjects) { SavedObjectUtilities.LoadSavedObject(item); } SaveManager.RecalculateAllClustersEverywhereWithDelay(); }, null); }
public static void CheckObject(GameObject gameObject) { if (gameObject == null || gameObject.tag == "World" || gameObject.tag == "Wire" || gameObject.tag == "CircuitBoard") { return; } var savedObj = SavedObjectUtilities.CreateSavedObjectFrom(gameObject); var undoItem = new ComponentUndoItem { SavedObject = savedObj, Parent = gameObject.transform.parent }; foreach (var input in gameObject.GetComponentsInChildren <CircuitInput>()) { undoItem.Wires.AddRange(input.IIConnections.Cast <Wire>()); undoItem.Wires.AddRange(input.IOConnections.Cast <Wire>()); } foreach (var input in gameObject.GetComponentsInChildren <CircuitOutput>()) { undoItem.Wires.AddRange(input.GetIOConnections().Cast <Wire>()); } UndoMod.UndoStack.Push(undoItem); }
public static PlaceComponentPacket BuildFromLocalComponent(GameObject component) { var netObj = component.GetComponent<NetObject>(); if (netObj == null) { netObj = component.AddComponent<NetObject>(); netObj.NetID = NetObject.GetNewID(); if (component.GetComponent<ObjectInfo>()?.ComponentType == ComponentType.Mount) { var board = component.GetComponentInChildren<CircuitBoard>(); board.gameObject.AddComponent<NetObject>().NetID = NetObject.GetNewID(); } } var objInfo = component.GetComponent<ObjectInfo>(); int parentId = component.transform.parent?.GetComponent<NetObject>()?.NetID ?? 0; return new PlaceComponentPacket { NetID = netObj.NetID, SavedObject = SavedObjectUtilities.CreateSavedObjectFrom(objInfo), LocalPosition = component.transform.localPosition, EulerAngles = component.transform.localEulerAngles, ParentBoardID = parentId }; }
public static void LoadAll() { MegaMeshManager.ClearReferences(); BehaviorManager.AllowedToUpdate = false; // don't let circuitry updates f**k us up while we're loading the game BehaviorManager.ClearAllLists(); // remove all existing stuff in the world before loading the new stuff ObjectInfo[] saveobjects = UnityEngine.Object.FindObjectsOfType <ObjectInfo>(); // all active instances of SaveObject foreach (ObjectInfo save in saveobjects) { UnityEngine.Object.Destroy(save.gameObject); } List <SavedObjectV2> TopLevelObjects = (List <SavedObjectV2>)FileUtilities.LoadFromFile(RegionsPath, "world.tung"); if (TopLevelObjects != null) { foreach (SavedObjectV2 save in TopLevelObjects) { SavedObjectUtilities.LoadSavedObject(save); } } RecalculateAllClustersEverywhereWithDelay(); try { LoadPlayerPosition(); } catch { ES3.DeleteFile(PlayersPath + "/player"); } GameplayUIManager.UIState = UIState.None; }
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 static List <SavedObjectV2> GetTopLevelObjects() { List <SavedObjectV2> TopLevelObjects = new List <SavedObjectV2>(); foreach (ObjectInfo save in ActiveSaveObjects) { if (save.transform.parent == null) { TopLevelObjects.Add(SavedObjectUtilities.CreateSavedObjectFrom(save)); } } return(TopLevelObjects); }
public override void Do() { var parentBoard = NetObject.GetByNetId(Packet.ParentBoardID); var component = SavedObjectUtilities.LoadSavedObject(Packet.SavedObject, parentBoard?.transform); component.AddComponent <NetObject>().NetID = Packet.NetID; if (Network.IsServer) { foreach (var item in component.GetComponentsInChildren <CircuitOutput>()) { CircuitStatePacket.SetOutputState(item, item.On, true); } } }
public override void Undo() { IGConsole.Log("Component SavedObject is null: " + (SavedObject == null)); SavedObjectUtilities.LoadSavedObject(SavedObject, Parent); //foreach (var item in Wires) //{ // if (item is InputInputConnection ii) // { // StuffConnector.LinkInputs(ii); // } // else if (item is InputOutputConnection io) // { // StuffConnector.LinkInputOutput(io); // } //} }
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; }
public override void Do() { NetObject parentBoard = NetObject.GetByNetId(Packet.ParentBoardID); SavedCircuitBoard savedBoard; using (var mem = new MemoryStream(Packet.SavedBoard)) { savedBoard = (SavedCircuitBoard)BinFormatter.Deserialize(mem); } var boardObj = SavedObjectUtilities.LoadSavedObject(savedBoard, parentBoard?.transform); boardObj.transform.position = Packet.Position; boardObj.transform.eulerAngles = Packet.EulerAngles; foreach (var item in boardObj.GetComponentsInChildren <CircuitOutput>()) { item.On = item.On; //It works 100% of the times most of the time } BoardFunctions.RecalculateClustersOfBoard(boardObj); SnappingPeg.TryToSnapIn(boardObj); }