示例#1
0
        /// <summary>
        /// Helper function that uses serialized data from asset bundle to setup prefab
        /// </summary>
        /// <param name="prefab">prefab object to setup</param>
        /// <param name="recordDictionary">serialized data</param>
        /// <returns></returns>
        public GameObject SetupPrefabHelper(GameObject prefab, Dictionary <string, List <SerializedRecord> > recordDictionary)
        {
            if (prefab == null || recordDictionary == null)
            {
                Debug.LogError("Failed to setup prefab - either the prefab or the deserialized dictionary was null - stopping");
                return(null);
            }

            List <Transform> transforms = new List <Transform>();

            ModManager.GetAllChildren(prefab.transform, ref transforms);

            for (int i = 0; i < transforms.Count; i++)
            {
                GameObject go = transforms[i].gameObject;

                if (recordDictionary.ContainsKey(go.name))
                {
                    List <SerializedRecord> records = recordDictionary[go.name];

                    for (int j = 0; j < records.Count; j++)
                    {
                        SerializedRecord    sr = records[j];
                        Component           co = go.AddComponent(sr.componentType);
                        Idfmod_Serializable isCustomSerializable = co as Idfmod_Serializable;

                        if (isCustomSerializable == null)
                        {
                            continue;
                        }
                        if (sr.serializedObjects == null || sr.serializedObjects.Length < 1)
                        {
                            continue;
                        }

                        isCustomSerializable.Deseralized(sr.serializedObjects);
                    }
                }
            }
            return(prefab);
        }
示例#2
0
        private string[] GetAllAssetNames()
        {
            try
            {
                if (assetBundle == null)
                {
                    return(null);
                }

                string[] assetNames = assetBundle.GetAllAssetNames();
                for (int i = 0; i < assetNames.Length; i++)
                {
                    assetNames[i] = ModManager.GetAssetName(assetNames[i]);
                }

                return(assetNames);
            }
            catch (Exception ex)
            {
                Debug.Log(ex.Message);
                return(null);
            }
        }
示例#3
0
        private bool AddAsset(string assetName, LoadedAsset la)
        {
            if (IsAssetLoaded(assetName))
            {
                return(false);
            }
            else if (la.Obj == null || la.T == null)
            {
                return(false);
            }
            else
            {
                LoadedAssets.Add(assetName, la);

                if (this.modInfo != null && this.Name != null)
                {
                    ModManager.OnLoadAsset(this.Name, assetName, la.T);
                }
#if DEBUG
                Debug.Log(string.Format("added asset: {0}", assetName));
#endif
                return(true);
            }
        }
示例#4
0
 /// <summary>
 /// Checks if this mod is expected to run on current version of Daggerfall Unity.
 /// </summary>
 /// <returns>True if game version is satisfied, false if is not, null if unknown.</returns>
 internal bool?IsGameVersionSatisfied()
 {
     return(ModManager.IsVersionLowerOrEqual(ModInfo.DFUnity_Version, VersionInfo.DaggerfallUnityVersion));
 }
示例#5
0
        /// <summary>
        /// Will retrieve asset either from LoadedAssets or asset bundle and return it. Will load
        /// asset bundle if necessary.
        /// </summary>
        /// <typeparam name="T">Type of asset</typeparam>
        /// <param name="assetName">name of asset</param>
        /// <param name="loadedBundle">had to load asset bundle</param>
        /// <returns>A reference to the loaded asset or null if not found.</returns>
        private T LoadAssetFromBundle <T>(string assetName, out bool loadedBundle) where T : UnityEngine.Object
        {
            LoadedAsset la = new LoadedAsset();

            loadedBundle = false;

            if (string.IsNullOrEmpty(assetName))
            {
                return(null);
            }
            try
            {
                assetName = ModManager.GetAssetName(assetName);
                float time = Time.realtimeSinceStartup;

                if (IsAssetLoaded(assetName))
                {
                    la = loadedAssets[assetName];
                    if (la.Obj)
                    {
                        // Update timestamp of last access, but only if difference is
                        // significant to limit the number of reassignment to dictionary.
                        if (la.TimeStamp < 0 || time - la.TimeStamp > 59)
                        {
                            la.TimeStamp            = time;
                            loadedAssets[assetName] = la;
                        }

                        return(la.Obj as T);
                    }

                    loadedAssets.Remove(assetName);
                    Debug.LogWarningFormat("Removed asset {0} from cache of mod {1} because object is unloaded.", assetName, Title);
                }

#if UNITY_EDITOR
                if (IsVirtual)
                {
                    la.Obj = LoadAssetFromResources <T>(assetName);
                    if (la.Obj != null)
                    {
                        la.T         = la.Obj.GetType();
                        la.TimeStamp = time;
                        loadedAssets.Add(assetName, la);
                    }
                    return(la.Obj as T);
                }
#endif

                if (AssetBundle == null)
                {
                    loadedBundle = LoadAssetBundle();
                }

                if (AssetBundle.Contains(assetName))
                {
                    la.Obj = AssetBundle.LoadAsset <T>(assetName);

                    if (la.Obj != null)
                    {
                        la.T         = la.Obj.GetType();
                        la.TimeStamp = time;
                        AddAsset(assetName, la);
                    }
                    return(la.Obj as T);
                }
                else
                {
                    return(null);
                }
            }
            catch (Exception ex)
            {
                Debug.LogError(ex.Message);
                return(null);
            }
        }
示例#6
0
        /// <summary>
        /// Serializes components on gameobject & children w/ Idfmod_Serializable interface
        /// WARNING: This will destroy any serialized component from prefab
        /// </summary>
        /// <param name="prefab">base prefab</param>
        /// <param name="serialized">serialized string</param>
        /// <returns></returns>
        private static bool SerializePrefabHelper(GameObject prefab, out string serialized)
        {
            serialized = "";

            Dictionary <string, List <SerializedRecord> > recordDictionary = new Dictionary <string, List <SerializedRecord> >();
            List <Transform> transforms = new List <Transform>();

            ModManager.GetAllChildren(prefab.transform, ref transforms);

            for (int i = 0; i < transforms.Count; i++)
            {
                if (transforms[i] == null)
                {
                    continue;
                }

                GameObject go = transforms[i].gameObject;
                List <SerializedRecord> serializedRecords = new List <SerializedRecord>();
                Component[]             components        = go.GetComponents <Component>();

                for (int j = 0; j < components.Length; j++)
                {
                    if (components[j] == null)
                    {
                        continue;
                    }

                    Component           co            = components[j];
                    Idfmod_Serializable sModInterface = co as Idfmod_Serializable;

                    if (sModInterface == null)
                    {
                        continue;
                    }
                    else if (sModInterface.Ignore)
                    {
                        continue;
                    }

                    object[] toSerialize = sModInterface.ToSerialize();
                    if (toSerialize != null)
                    {
                        for (int k = 0; k < toSerialize.Length; k++)
                        {
                            if (toSerialize[k].GetType().IsSubclassOf(typeof(Component)))
                            {
                                Debug.LogError("Can't serialize monobehaviours: " + toSerialize[k].ToString());
                                return(false);
                            }
                        }
                    }

                    SerializedRecord sr = new SerializedRecord(go.name, co, toSerialize);
                    serializedRecords.Add(sr);
                    UnityEngine.Object.DestroyImmediate(co, true);
                }

                if (serializedRecords.Count > 0)
                {
                    if (recordDictionary.ContainsKey(go.name))
                    {
                        Debug.LogWarning("Please make sure all game objects have unique names, can't serialize objects for: " + go.name);
                        continue;
                    }
                    else
                    {
                        recordDictionary.Add(go.name, serializedRecords);
                    }
                }
            }

            FullSerializer.fsData   sData;
            FullSerializer.fsResult result = ModManager._serializer.TrySerialize(typeof(Dictionary <string, List <SerializedRecord> >), recordDictionary, out sData).AssertSuccessWithoutWarnings();

            serialized = FullSerializer.fsJsonPrinter.PrettyJson(sData);
            return(result.Succeeded);
        }