// Find matching signature in data pool SmoothSaveDataSource FindDataSource(SmoothSaveDataSource newDataSource) { // Check for empy data pool if (_dataPool == null) { return(null); } else if (_dataPool.DataSources == null) { return(null); } else if (_dataPool.DataSources.Count == 0) { return(null); } // Check each data source foreach (var currentDataSource in _dataPool.DataSources) { if (currentDataSource.MatchSignature(newDataSource)) { return(currentDataSource); } } return(null); }
// Perform an action with an FSM variable protected bool DoVariableAction(VariableAction variableAction, NamedVariable fsmVariable, string key, bool?isGlobal = null) { // Check data if (fsmVariable == null) { throw new ArgumentNullException("fsmVariable", "Cannot do action with null variable: " + variableAction); } // If no current pool, get one if (_dataPool == null) { DefaultDataPool(); } // Get global state if (isGlobal == null) { isGlobal = FsmVariables.GlobalVariables.Contains(fsmVariable); } // If GameObject, do action and exit if (fsmVariable.VariableType == VariableType.GameObject) { return(DoGameObjectAction(variableAction, (FsmGameObject)fsmVariable, key, (bool)isGlobal)); } // If array of GameObjects, do actions and exit if (fsmVariable.VariableType == VariableType.Array) { if (((FsmArray)fsmVariable).ElementType == VariableType.GameObject) { var allSuccess = true; for (int index = 0; index < ((FsmArray)fsmVariable).Values.Length; index++) { if (!DoGameObjectAction(variableAction, new FsmGameObject(fsmVariable.Name) { Value = (GameObject)((FsmArray)fsmVariable).Values[index] }, key, (bool)isGlobal, index)) { allSuccess = false; } } return(allSuccess); } } // Search data pool for new data source var newDataSource = new SmoothSaveDataSource(fsmVariable, key, (bool)isGlobal, Fsm); var oldDataSource = FindDataSource(newDataSource); // Copy variable to pool if (variableAction == VariableAction.CopyVariableToPool) { // If found, remove old copy if (oldDataSource != null) { _dataPool.DataSources.Remove(oldDataSource); } _dataPool.DataSources.Add(newDataSource); return(true); } // Copy pool to variable else if (variableAction == VariableAction.CopyPoolToVariable) { if (oldDataSource != null) { // If no FSM variable, set it if (oldDataSource.GetFsmVariable() == null) { oldDataSource.SetFsmVariable(newDataSource); } oldDataSource.Deserialize(); return(true); } else { Debug.Log("Variable data not found in pool: " + fsmVariable.Name, Owner); return(false); } } else { throw new ArgumentException("Invalid variable action: " + variableAction); } }