Пример #1
0
        private static void LoadBuiltinAssets()
        {
            var npcSettings = NPCSettings.Load();
            var prefabs     = new[]
            {
                "Hatchback",
                "Sedan",
                "Jeep",
                "SUV",
                "BoxTruck",
                "SchoolBus",
            };

            foreach (var entry in prefabs)
            {
                var go = npcSettings.NPCPrefabs.Find(x => x.name == entry) as GameObject;
                if (go == null)
                {
                    // I was seeing this in editor a few times, where it was not able to find the builtin assets
                    Debug.LogError($"Failed to load builtin {entry} " + (go == null?"null":go.ToString()));
                    continue;
                }
                Map.NPCSizeType size = Map.NPCSizeType.MidSize;
                var             meta = go.GetComponent <NPCMetaData>();

                if (meta != null)
                {
                    size = meta.SizeType;
                }
                else
                {
                    Debug.LogWarning($"NPC {entry} missing meta info, setting default size");
                }

                NPCVehicles.Add(entry, new NPCAssetData
                {
                    Prefab    = go,
                    NPCType   = size,
                    Name      = entry,
                    AssetGuid = $"builtin-{entry}",
                });
            }

            var behaviours = new []
            {
                typeof(NPCLaneFollowBehaviour),
                typeof(NPCWaypointBehaviour),
                typeof(NPCManualBehaviour),
            };

            foreach (var b in behaviours)
            {
                NPCBehaviours.Add(b.ToString(), b);
            }
        }
Пример #2
0
        private static void loadNPCAsset(Manifest manifest, VfsEntry dir)
        {
            if (manifest.bundleFormat != BundleConfig.Versions[BundleConfig.BundleTypes.NPC])
            {
                throw new Exception($"manifest version mismatch, expected {BundleConfig.Versions[BundleConfig.BundleTypes.NPC]}, got {manifest.bundleFormat}");
            }
            Assembly pluginSource = loadAssembly(dir, $"{manifest.assetName}.dll");

            if (pluginSource != null)
            {
                foreach (Type ty in pluginSource.GetTypes())
                {
                    if (ty.IsAbstract)
                    {
                        continue;
                    }
                    if (typeof(NPCBehaviourBase).IsAssignableFrom(ty))
                    {
                        NPCBehaviours.Add(ty.ToString(), ty);
                    }
                    else if (typeof(ICommand).IsAssignableFrom(ty))
                    {
                        var cmd = Activator.CreateInstance(ty) as ICommand;
                        ApiManager.Commands.Add(cmd.Name, cmd);
                    }
                }
            }
            var         texEntry      = dir.Find($"{manifest.assetGuid}_npc_textures");
            AssetBundle textureBundle = null;

            if (texEntry != null)
            {
                var texStream = VirtualFileSystem.VirtualFileSystem.EnsureSeekable(texEntry.SeekableStream(), (int)texEntry.Size);
                textureBundle = AssetBundle.LoadFromStream(texStream, 0, 1 << 20);
            }

            string platform    = SystemInfo.operatingSystemFamily == OperatingSystemFamily.Windows ? "windows" : "linux";
            var    pluginEntry = dir.Find($"{manifest.assetGuid}_npc_main_{platform}");

            if (pluginEntry != null)
            {
                AssetBundle pluginBundle = AssetBundle.LoadFromStream(pluginEntry.SeekableStream());
                var         pluginAssets = pluginBundle.GetAllAssetNames();
                GameObject  prefab       = pluginBundle.LoadAsset <GameObject>(pluginAssets[0]);

                Map.NPCSizeType size = Map.NPCSizeType.MidSize;
                var             meta = prefab.GetComponent <NPCMetaData>();

                if (meta != null)
                {
                    size = meta.SizeType;
                }
                else
                {
                    Debug.LogWarning($"NPC {manifest.assetName} missing meta info, setting default type");
                }

                NPCVehicles.Add(manifest.assetName, new NPCAssetData()
                {
                    prefab    = prefab,
                    Name      = manifest.assetName,
                    AssetGuid = manifest.assetGuid,
                    NPCType   = size,
                });
            }

            if (pluginEntry == null && pluginSource == null)
            {
                Debug.LogError("Neither assembly nor prefab found in " + manifest.assetName);
            }

            if (textureBundle && !AssetBundle.GetAllLoadedAssetBundles().Contains(textureBundle))
            {
                textureBundle.LoadAllAssets();
            }
        }
Пример #3
0
        private static void LoadNPCAsset(Manifest manifest, VfsEntry dir)
        {
            if (manifest.assetFormat != BundleConfig.Versions[BundleConfig.BundleTypes.NPC])
            {
                throw new Exception($"manifest version mismatch, expected {BundleConfig.Versions[BundleConfig.BundleTypes.NPC]}, got {manifest.assetFormat}");
            }
            Assembly pluginSource = LoadAssembly(dir, $"{manifest.assetName}.dll");

            if (pluginSource != null)
            {
                foreach (Type ty in pluginSource.GetTypes())
                {
                    if (ty.IsAbstract)
                    {
                        continue;
                    }
                    if (typeof(NPCBehaviourBase).IsAssignableFrom(ty))
                    {
                        NPCBehaviours.Add(ty.ToString(), ty);
                    }
                    else if (typeof(ICommand).IsAssignableFrom(ty))
                    {
                        var cmd = Activator.CreateInstance(ty) as ICommand;
                        ApiManager.Commands.Add(cmd.Name, cmd);
                    }
                }
            }

            var         texEntry      = dir.Find($"{manifest.assetGuid}_npc_textures");
            AssetBundle textureBundle = null;

            if (texEntry != null)
            {
                var texStream = VirtualFileSystem.VirtualFileSystem.EnsureSeekable(texEntry.SeekableStream(), (int)texEntry.Size);
                textureBundle = AssetBundle.LoadFromStream(texStream, 0, 1 << 20);
            }

            string platform    = SystemInfo.operatingSystemFamily == OperatingSystemFamily.Windows ? "windows" : "linux";
            var    pluginEntry = dir.Find($"{manifest.assetGuid}_npc_main_{platform}");

#if UNITY_EDITOR
            if (EditorPrefs.GetBool("Simulator/Developer Debug Mode", false) == true)
            {
                foreach (var NPCDir in Directory.EnumerateDirectories(Path.Combine(BundleConfig.ExternalBase, BundleConfig.pluralOf(BundleConfig.BundleTypes.NPC))))
                {
                    var assembly = Assembly.Load("Simulator.NPCs");
                    if (File.Exists(Path.Combine(NPCDir, manifest.assetName, $"{manifest.assetName}.prefab")))
                    {
                        var prefab = (GameObject)AssetDatabase.LoadAssetAtPath(Path.Combine(NPCDir, manifest.assetName, $"{manifest.assetName}.prefab"), typeof(GameObject));

                        Map.NPCSizeType size = Map.NPCSizeType.MidSize;
                        var             meta = prefab.GetComponent <NPCMetaData>();

                        if (meta != null)
                        {
                            size = meta.SizeType;
                        }
                        else
                        {
                            Debug.LogWarning($"NPC {manifest.assetName} missing meta info, setting default type");
                        }

                        NPCVehicles.Add(manifest.assetName, new NPCAssetData()
                        {
                            Prefab    = prefab,
                            Name      = manifest.assetName,
                            AssetGuid = manifest.assetGuid,
                            NPCType   = size,
                        });

                        if (textureBundle && !AssetBundle.GetAllLoadedAssetBundles().Contains(textureBundle))
                        {
                            textureBundle.LoadAllAssets();
                        }

                        return;
                    }
                }
            }
#endif

            if (pluginEntry != null)
            {
                AssetBundle pluginBundle   = AssetBundle.LoadFromStream(pluginEntry.SeekableStream());
                var         pluginAssets   = pluginBundle.GetAllAssetNames();
                var         prefabName     = $"{manifest.assetName}.prefab";
                var         mainPrefabName =
                    pluginAssets.First(name => name.IndexOf(prefabName, StringComparison.InvariantCultureIgnoreCase) >= 0);
                GameObject prefab = pluginBundle.LoadAsset <GameObject>(mainPrefabName);

                Map.NPCSizeType size = Map.NPCSizeType.MidSize;
                var             meta = prefab.GetComponent <NPCMetaData>();

                if (meta != null)
                {
                    size = meta.SizeType;
                }
                else
                {
                    Debug.LogWarning($"NPC {manifest.assetName} missing meta info, setting default type");
                }

                NPCVehicles.Add(manifest.assetName, new NPCAssetData()
                {
                    Prefab    = prefab,
                    Name      = manifest.assetName,
                    AssetGuid = manifest.assetGuid,
                    NPCType   = size,
                });
            }

            if (pluginEntry == null && pluginSource == null)
            {
                Debug.LogWarning("Neither assembly nor prefab found in " + manifest.assetName);
            }

            if (textureBundle && !AssetBundle.GetAllLoadedAssetBundles().Contains(textureBundle))
            {
                textureBundle.LoadAllAssets();
            }
        }