public void ResolvePointerReferences() { // Go through PointerReferenceList and resolve all pointers. foreach (var pointerReference in PointerReferenceList) { // Only act if not null. Otherwise placeholder functions already wrote zeroes and that's good enough. if (pointerReference.Reference != null) { // Check if this object was committed so we can resolve the pointer. var objectReference = ObjectReferences.Where(kv => ReferenceEquals(kv.Key, pointerReference.Reference)); if (!objectReference.Any()) { throw new KeyNotFoundException($"Failed to resolve a committed reference to a {pointerReference.Reference.GetType().Name}"); } // Resolve pointer var patchLocation = pointerReference.Location; var objectLocation = objectReference.Single().Value; var relativeOffset = objectLocation - patchLocation; Utility.PushWritePosition(); Utility.SetWritePosition(patchLocation); Utility.Write(relativeOffset); Utility.PopWritePosition(); } } PointerReferenceList.Clear(); }
public void Serialize(Stream stream, object graph) { #if !(RELEASE && RELEASE_DISABLE_CHECKS) if (stream == null) { throw new ArgumentNullException("stream"); } if (graph == null) { throw new ArgumentNullException("graph"); } #endif Writer = new StreamWriter(stream) { AutoFlush = true }; Reader = null; ObjectReferences.Clear(); m_currentLine = 0; StartWrite(new ObjectReference("root", graph)); stream.Seek(0, SeekOrigin.Begin); }
private void Awake() { if (Instance == null) { Instance = this; } GetTilemapReferences(); GetUserInterfaceReferences(); GetDialogueReferences(); }
/// <summary> /// Starts reading an reference that was written with <see cref="StartWrite"/>. /// </summary> /// <returns></returns> public ObjectReference StartRead() { var name = ReadLine(); int line = m_currentLine; var serializationType = (SerializationType)Enum.Parse(typeof(SerializationType), ReadLine()); var objReference = new ObjectReference(name, serializationType); if (serializationType > SerializationType.ReferenceTypes) { ObjectReferences.Add(line, objReference); } switch (serializationType) { case SerializationType.Null: break; case SerializationType.Reference: ReadReference(objReference); break; case SerializationType.Object: ReadObject(objReference); break; case SerializationType.Array: ReadArray(objReference); break; case SerializationType.GenericEnumerable: ReadGenericEnumerable(objReference); break; case SerializationType.Enumerable: ReadEnumerable(objReference); break; case SerializationType.Enum: ReadEnum(objReference); break; case SerializationType.Any: ReadAny(objReference); break; case SerializationType.String: ReadString(objReference); break; case SerializationType.MemberInfo: ReadMemberInfo(objReference); break; case SerializationType.Type: ReadType(objReference); break; case SerializationType.Delegate: ReadDelegate(objReference); break; case SerializationType.IntPtr: ReadIntPtr(objReference); break; case SerializationType.UnusedMarker: ReadUnusedMarker(objReference); break; } #if !(RELEASE && RELEASE_DISABLE_CHECKS) if (!objReference.AllowNull && objReference.Value == null && serializationType != SerializationType.Null) { throw new SerializationException(string.Format("Failed to deserialize object of type {0} {1} at line {2}!", objReference.SerializationType, objReference.Name, line)); } #endif return(objReference); }
private void Awake() { if (instance != null) { Destroy(gameObject); return; } fixedTimeScale = Time.fixedDeltaTime; instance = this; DontDestroyOnLoad(this); }
void ReadReference(ObjectReference objReference) { int referenceLine = int.Parse(ReadLine()); ObjectReference originalReference; if (!ObjectReferences.TryGetValue(referenceLine, out originalReference)) { throw new SerializationException(string.Format("Failed to obtain reference {0} at line {1}! Last line was {2})", objReference.Name, referenceLine, m_currentLine)); } objReference.Value = originalReference.Value; objReference.AllowNull = originalReference.AllowNull; }
// This sets the reference point to an object, assuming it's about to be written. // Used for non-ISaveable types, generally list objects. Enforces the fact you need // to write it immediately for this reference to be valid by "writeAction." public void SaveAndMarkReference(object obj, Action writeAction) { if (obj != null) { var objectReference = ObjectReferences.Where(kv => ReferenceEquals(kv.Key, obj)); if (objectReference.Any()) { throw new InvalidOperationException($"A reference to this {obj.GetType().Name} has already been made!"); } ObjectReferences[obj] = Utility.GetWritePosition(); writeAction(); } }
public object Deserialize(Stream stream) { #if !(RELEASE && RELEASE_DISABLE_CHECKS) if (stream == null || stream.Length == 0) { throw new ArgumentNullException("stream"); } #endif Reader = new StreamReader(stream); ObjectReferences.Clear(); CurrentLine = 0; return(StartRead().Value); }
public HelperMethods( ModuleDefinition moduleDefinition, EnumReferences enumReferences, ExceptionReferences exceptionReferences, NullableReferences nullableReferences, ObjectReferences objectReferences, ReflectionReferences reflectionReferences, StringReferences stringReferences) { this.moduleDefinition = moduleDefinition; this.enumReferences = enumReferences; this.exceptionReferences = exceptionReferences; this.objectReferences = objectReferences; this.nullableReferences = nullableReferences; this.reflectionReferences = reflectionReferences; this.stringReferences = stringReferences; }
/// <summary> /// Checks if this object has already been serialized. /// </summary> /// <param name="objectReference"></param> /// <returns>true if object had already been serialized.</returns> bool TryWriteReference(ObjectReference objectReference) { if (objectReference.SerializationType > SerializationType.ReferenceTypes) { foreach (var pair in ObjectReferences) { if (pair.Value.Value.Equals(objectReference.Value)) { WriteReference(objectReference, pair.Key); return(true); } } ObjectReferences.Add(m_currentLine, objectReference); } return(false); }
/// <summary> /// Removes the objects specified by the object references object. /// </summary> private static void ProcessObjectReferences(ObjectReferences objectReferences, bool fromScene) { if (objectReferences == null) { return; } RemoveObjects(objectReferences.RemoveObjects); objectReferences.RemoveObjects = null; #if !FIRST_PERSON_CONTROLLER RemoveObjects(objectReferences.FirstPersonObjects); objectReferences.FirstPersonObjects = null; #endif #if !THIRD_PERSON_CONTROLLER RemoveObjects(objectReferences.ThirdPersonObjects); objectReferences.ThirdPersonObjects = null; #endif #if !ULTIMATE_CHARACTER_CONTROLLER_SHOOTER RemoveObjects(objectReferences.ShooterObjects); objectReferences.ShooterObjects = null; #endif #if !ULTIMATE_CHARACTER_CONTROLLER_MELEE RemoveObjects(objectReferences.MeleeObjects); objectReferences.MeleeObjects = null; #endif #if !FIRST_PERSON_CONTROLLER || !THIRD_PERSON_CONTROLLER // Remove any view types and states that are no longer valid. Opsive.UltimateCharacterController.Camera.CameraController cameraController; if (fromScene) { cameraController = GameObject.FindObjectOfType <UltimateCharacterController.Camera.CameraController>(); } else { cameraController = objectReferences.GetComponent <UltimateCharacterController.Camera.CameraController>(); } if (cameraController != null) { cameraController.DeserializeViewTypes(); var viewTypes = new List <ViewType>(cameraController.ViewTypes); for (int i = viewTypes.Count - 1; i > -1; --i) { if (viewTypes[i] == null) { viewTypes.RemoveAt(i); continue; } viewTypes[i].States = RemoveUnusedStates(viewTypes[i].States); } cameraController.ViewTypeData = Serialization.Serialize <ViewType>(viewTypes); cameraController.ViewTypes = viewTypes.ToArray(); InspectorUtility.SetDirty(cameraController); } // Remove any movement types and states that are no longer valid. Character.UltimateCharacterLocomotion characterLocomotion; if (fromScene) { characterLocomotion = GameObject.FindObjectOfType <Character.UltimateCharacterLocomotion>(); } else { characterLocomotion = objectReferences.GetComponent <Character.UltimateCharacterLocomotion>(); } if (characterLocomotion != null) { characterLocomotion.DeserializeMovementTypes(); var movementTypes = new List <MovementType>(characterLocomotion.MovementTypes); for (int i = movementTypes.Count - 1; i > -1; --i) { if (movementTypes[i] == null) { movementTypes.RemoveAt(i); continue; } movementTypes[i].States = RemoveUnusedStates(movementTypes[i].States); } characterLocomotion.MovementTypeData = Serialization.Serialize <MovementType>(movementTypes); characterLocomotion.MovementTypes = movementTypes.ToArray(); #if FIRST_PERSON_CONTROLLER characterLocomotion.SetMovementType(UltimateCharacterController.Utility.UnityEngineUtility.GetType(characterLocomotion.FirstPersonMovementTypeFullName)); #else characterLocomotion.SetMovementType(UltimateCharacterController.Utility.UnityEngineUtility.GetType(characterLocomotion.ThirdPersonMovementTypeFullName)); #endif // Ensure the animator is pointing to the correct reference. var animator = characterLocomotion.GetComponent <Animator>(); if (animator != null && animator.runtimeAnimatorController == null) { animator.runtimeAnimatorController = ManagerUtility.FindAnimatorController(null); InspectorUtility.SetDirty(animator); } // Check for unused ability states. var abilities = new List <Ability>(characterLocomotion.GetSerializedAbilities()); for (int i = abilities.Count - 1; i > -1; --i) { if (abilities[i] == null) { abilities.RemoveAt(i); continue; } abilities[i].States = RemoveUnusedStates(abilities[i].States); } characterLocomotion.AbilityData = Serialization.Serialize <Ability>(abilities); characterLocomotion.Abilities = abilities.ToArray(); // Check for unused item ability states. var itemAbilities = new List <ItemAbility>(characterLocomotion.GetSerializedItemAbilities()); for (int i = itemAbilities.Count - 1; i > -1; --i) { if (itemAbilities[i] == null) { itemAbilities.RemoveAt(i); continue; } itemAbilities[i].States = RemoveUnusedStates(itemAbilities[i].States); } characterLocomotion.ItemAbilityData = Serialization.Serialize <ItemAbility>(itemAbilities); characterLocomotion.ItemAbilities = itemAbilities.ToArray(); InspectorUtility.SetDirty(characterLocomotion); // Update the inventory. var inventory = characterLocomotion.GetComponent <Inventory.Inventory>(); var loadout = new List <Inventory.ItemDefinitionAmount>(inventory.DefaultLoadout); for (int i = loadout.Count - 1; i > -1; --i) { if (loadout[i] == null) { loadout.RemoveAt(i); } } inventory.DefaultLoadout = loadout.ToArray(); InspectorUtility.SetDirty(inventory); var itemSetManager = characterLocomotion.GetComponent <Inventory.ItemSetManager>(); if (itemSetManager != null) { var categoryItemSets = itemSetManager.CategoryItemSets; for (int i = 0; i < categoryItemSets.Length; ++i) { for (int j = categoryItemSets[i].ItemSetList.Count - 1; j > -1; --j) { var nullItemIdentifier = true; for (int k = 0; k < categoryItemSets[i].ItemSetList[j].Slots.Length; ++k) { if (categoryItemSets[i].ItemSetList[j].Slots[k] != null) { nullItemIdentifier = false; break; } } if (nullItemIdentifier) { categoryItemSets[i].ItemSetList.RemoveAt(j); } } } ; InspectorUtility.SetDirty(itemSetManager); } } #if !THIRD_PERSON_CONTROLLER // Set the shadow caster for the first person only objects. var shadowCaster = ManagerUtility.FindInvisibleShadowCaster(null); if (shadowCaster != null) { for (int i = 0; i < objectReferences.ShadowCasterObjects.Length; ++i) { if (objectReferences.ShadowCasterObjects[i] == null) { continue; } var renderers = objectReferences.ShadowCasterObjects[i].GetComponentsInChildren <Renderer>(); for (int j = 0; j < renderers.Length; ++j) { var materials = renderers[j].sharedMaterials; for (int k = 0; k < materials.Length; ++k) { materials[k] = shadowCaster; } renderers[j].sharedMaterials = materials; InspectorUtility.SetDirty(renderers[j]); } } } #endif var items = objectReferences.GetComponentsInChildren <Items.Item>(); for (int i = 0; i < items.Length; ++i) { CheckItem(items[i].gameObject); } // Ensure all of the states point to a preset StateBehavior[] stateBehaviors; if (fromScene) { stateBehaviors = GameObject.FindObjectsOfType <StateBehavior>(); } else { stateBehaviors = objectReferences.GetComponentsInChildren <StateBehavior>(true); } if (stateBehaviors != null) { for (int i = 0; i < stateBehaviors.Length; ++i) { stateBehaviors[i].States = RemoveUnusedStates(stateBehaviors[i].States); InspectorUtility.SetDirty(stateBehaviors[i]); } } #endif // Some doors should be locked. #if !FIRST_PERSON_CONTROLLER LockDoors(objectReferences.FirstPersonDoors); #endif #if !THIRD_PERSON_CONTROLLER LockDoors(objectReferences.ThirdPersonDoors); #endif for (int i = 0; i < objectReferences.NestedReferences.Length; ++i) { GameObject nestedRoot = null; var nestedObject = objectReferences.NestedReferences[i]; if (PrefabUtility.IsPartOfPrefabAsset(nestedObject)) { nestedRoot = PrefabUtility.LoadPrefabContents(AssetDatabase.GetAssetPath(objectReferences.NestedReferences[i])); nestedObject = nestedRoot.GetComponent <ObjectReferences>(); } ProcessObjectReferences(nestedObject, false); if (nestedRoot != null) { PrefabUtility.SaveAsPrefabAsset(nestedRoot, AssetDatabase.GetAssetPath(objectReferences.NestedReferences[i])); PrefabUtility.UnloadPrefabContents(nestedRoot); } } UnpackPrefab(objectReferences); Object.DestroyImmediate(objectReferences, true); }
public GameContext(MonoBehaviour contextView, ContextStartupFlags flag, ObjectReferences objectReferences) : base(contextView, flag) { this.objectReferences = objectReferences; }
IEnumerator InitialisedProperty() { ///PropertyList.Clear(); yield return(null); try { int count = 0; //keeping track of number of index in the property list and assign into a PropertyDetail type of list foreach (GameObject g in property) { int id = g.GetComponent <PropertyDetails>().PropertyID; string[] strArray = propeList[count].Split('\t'); //implement it only for all the PROPERTY block in the board if (ObjectReferences.IsProperty(id)) { // DebugConsole.Log(propeList[count].ToString(), "normal"); string propertyName = strArray[0].ToString(); int propertyPrice = int.Parse(strArray[1]); int mortgage = int.Parse(strArray[2]); int rent = int.Parse(strArray[3]); int rent1 = int.Parse(strArray[4]); int rent2 = int.Parse(strArray[5]); int rent3 = int.Parse(strArray[6]); int rent4 = int.Parse(strArray[7]); int rent5 = int.Parse(strArray[8]); int houseCost = int.Parse(strArray[9]); int hotelCost = int.Parse(strArray[9]); g.GetComponent <PropertyDetails>().PropertyName = propertyName; g.GetComponent <PropertyDetails>().PropertyPrice = propertyPrice; g.GetComponent <PropertyDetails>().mortgage = mortgage; g.GetComponent <PropertyDetails>().rent = rent; g.GetComponent <PropertyDetails>().rent1 = rent1; g.GetComponent <PropertyDetails>().rent2 = rent2; g.GetComponent <PropertyDetails>().rent3 = rent3; g.GetComponent <PropertyDetails>().rent4 = rent4; g.GetComponent <PropertyDetails>().rent5 = rent5; g.GetComponent <PropertyDetails>().houseCost = houseCost; g.GetComponent <PropertyDetails>().hotelCost = hotelCost; //string tempPlayer = g.GetComponent<PropertyDetails>().PropertyOwner; g.GetComponent <PropertyDetails>().PropertyOwner = 0; //PropertyDetails p = new PropertyDetails(propertyName, id, propertyPrice, // mortgage, rent, rent1, rent2, rent3, rent4, rent5, houseCost, hotelCost, tempPlayer); //PropertyList.Add(p); count++; } // implement only for all the STATIONS if (id == ObjectReferences.STATION_ID) { //DebugConsole.Log(propeList[count].ToString(), "normal"); string propertyName = strArray[0].ToString(); int propertyPrice = int.Parse(strArray[1]); int mortgage = int.Parse(strArray[2]); int rent = int.Parse(strArray[3]); int rent1 = int.Parse(strArray[4]); int rent2 = int.Parse(strArray[5]); int rent3 = int.Parse(strArray[6]); g.GetComponent <PropertyDetails>().PropertyName = propertyName; g.GetComponent <PropertyDetails>().PropertyPrice = propertyPrice; g.GetComponent <PropertyDetails>().mortgage = mortgage; g.GetComponent <PropertyDetails>().rent = rent; g.GetComponent <PropertyDetails>().rent1 = rent1; g.GetComponent <PropertyDetails>().rent2 = rent2; g.GetComponent <PropertyDetails>().rent3 = rent3; g.GetComponent <PropertyDetails>().PropertyOwner = 0; count++; //Debug.Log("Initialised :" + count); } // only for all the UTILITIES if (id == ObjectReferences.UTILITIES_ID) { // DebugConsole.Log(propeList[count].ToString(),"normal"); string propertyName = strArray[0].ToString(); int propertyPrice = int.Parse(strArray[1]); int mortgage = int.Parse(strArray[2]); g.GetComponent <PropertyDetails>().PropertyName = propertyName; g.GetComponent <PropertyDetails>().PropertyPrice = propertyPrice; g.GetComponent <PropertyDetails>().mortgage = mortgage; g.GetComponent <PropertyDetails>().PropertyOwner = 0; count++; } } // end of loop } //end of try block catch (Exception e) { Debug.Log("Message Error: " + e.Message); } LoadFileInScene = false; Debug.Log("Initialised Completed"); }
private int FindExistingOrNewIndex(object obj) { if(_count == 0) { _items[0].Obj = obj; _count = 1; return 0; } int freeIndex = -1; int foundObjectCount = 0; int index; for (index = 0; index < _items.Length; index++) { // if found the object, just return the index if (ReferenceEquals(_items[index].Obj, obj)) { return index; } // the slot is free, remember it; we want to use the first free slot we found, if the object is not found later if (_items[index].ReferenceCount == 0) { if (freeIndex == -1) freeIndex = index; } // count number of objects found but not matching; if it's equal to number of objects in the array, we can exit the loop else { foundObjectCount++; if (foundObjectCount == _count) { index++; break; } } } if (freeIndex != -1) { _items[freeIndex].Obj = obj; _count++; return freeIndex; } // resize if (index == _items.Length) { var larger = new ObjectReferences[_items.Length << 1]; _items.CopyTo(larger, 0); _items = larger; } _items[index].Obj = obj; _count++; return index; }