示例#1
0
        public static GhostCodeGen.Status GenerateCollection(GhostCollectionAuthoringComponent collectionTarget, bool testOnly = false)
        {
            var serializerCodeGen   = new GhostCodeGen("Packages/com.unity.netcode/Editor/CodeGenTemplates/GhostSerializerCollection.cs");
            var deserializerCodeGen = new GhostCodeGen("Packages/com.unity.netcode/Editor/CodeGenTemplates/GhostDeserializerCollection.cs");

            var assetPath = GhostCodeGen.GetPrefabAssetPath(collectionTarget.gameObject);

            int ghostCount = 0;
            var namePrefix = collectionTarget.NamePrefix;

            var localReplacements = new Dictionary <string, string>();

            for (int i = 0; i < collectionTarget.Ghosts.Count; ++i)
            {
                var ghost = collectionTarget.Ghosts[i];
                if (ghost.prefab != null && ghost.enabled)
                {
                    ++ghostCount;
                    var serializerTypeName = ghost.prefab.name + "GhostSerializer";
                    var snapshotTypeName   = ghost.prefab.name + "SnapshotData";
                    var spawnerTypeName    = ghost.prefab.name + "GhostSpawnSystem";

                    localReplacements.Clear();
                    localReplacements.Add("GHOST_SERIALIZER_TYPE", serializerTypeName);
                    localReplacements.Add("GHOST_SNAPSHOT_TYPE", snapshotTypeName);
                    localReplacements.Add("GHOST_SPAWNER_TYPE", spawnerTypeName);
                    localReplacements.Add("GHOST_SERIALIZER_INDEX", i.ToString());
                    localReplacements.Add("GHOST_COLLECTION_PREFIX", namePrefix);

                    serializerCodeGen.GenerateFragment("GHOST_SERIALIZER_INSTANCE", localReplacements);
                    deserializerCodeGen.GenerateFragment("GHOST_DESERIALIZER_INSTANCE", localReplacements);

                    serializerCodeGen.GenerateFragment("GHOST_SERIALIZER_NAME", localReplacements);
                    serializerCodeGen.GenerateFragment("GHOST_FIND_TYPE", localReplacements);
                    serializerCodeGen.GenerateFragment("GHOST_BEGIN_SERIALIZE", localReplacements);
                    serializerCodeGen.GenerateFragment("GHOST_CALCULATE_IMPORTANCE", localReplacements);
                    serializerCodeGen.GenerateFragment("GHOST_SNAPSHOT_SIZE", localReplacements);
                    serializerCodeGen.GenerateFragment("GHOST_INVOKE_SERIALIZE", localReplacements);

                    deserializerCodeGen.GenerateFragment("GHOST_SERIALIZER_NAME", localReplacements);
                    deserializerCodeGen.GenerateFragment("GHOST_INITIALIZE_DESERIALIZE", localReplacements);
                    deserializerCodeGen.GenerateFragment("GHOST_BEGIN_DESERIALIZE", localReplacements);
                    deserializerCodeGen.GenerateFragment("GHOST_INVOKE_DESERIALIZE", localReplacements);
                    deserializerCodeGen.GenerateFragment("GHOST_INVOKE_SPAWN", localReplacements);
                }
            }

            var replacements = new Dictionary <string, string>();

            replacements.Add("GHOST_COLLECTION_PREFIX", namePrefix);
            replacements.Add("GHOST_SYSTEM_PREFIX", namePrefix);
            replacements.Add("GHOST_SERIALIZER_COUNT", ghostCount.ToString());
            var batch = new GhostCodeGen.Batch();

            serializerCodeGen.GenerateFile(assetPath, "", collectionTarget.SerializerCollectionPath, replacements, batch);
            deserializerCodeGen.GenerateFile(assetPath, "", collectionTarget.DeserializerCollectionPath, replacements, batch);
            bool didWrite = batch.Flush(testOnly);

            AssetDatabase.Refresh();
            return(didWrite ? GhostCodeGen.Status.Ok : GhostCodeGen.Status.NotModified);
        }
示例#2
0
        public static GhostCodeGen.Status GenerateGhost(GhostAuthoringComponent ghostInfo, bool testOnly = false)
        {
            var tempWorld = new World("GhostEnsureECSLoaded");

            tempWorld.Dispose();

            var allTypes   = TypeManager.GetAllTypes();
            var typeLookup = new Dictionary <string, Type>();

            foreach (var compType in allTypes)
            {
                if (compType.Type != null)
                {
                    typeLookup.Add(compType.Type.FullName, compType.Type);
                }
            }

            string ownerField = "";

            // Update type of all fields
            for (int comp = 0; comp < ghostInfo.Components.Length; ++comp)
            {
                if (!typeLookup.TryGetValue(ghostInfo.Components[comp].name, out var componentType))
                {
                    Debug.LogError($"Could not find the type {ghostInfo.Components[comp].name}");
                    return(GhostCodeGen.Status.Failed);
                }
                ghostInfo.Components[comp].NamespaceName = componentType.Namespace;
                ghostInfo.Components[comp].ShortName     = (String.IsNullOrEmpty(componentType.Namespace))
                    ? componentType.FullName
                    : componentType.FullName.Substring(componentType.Namespace.Length + 1);
                for (int field = 0; field < ghostInfo.Components[comp].fields.Length; ++field)
                {
                    var fieldInfo = componentType.GetField(ghostInfo.Components[comp].fields[field].name);
                    if (fieldInfo == null)
                    {
                        Debug.LogError("Could not find field: " + ghostInfo.Components[comp].fields[field].name +
                                       " in componentType: " + ghostInfo.Components[comp].name);
                        return(GhostCodeGen.Status.Failed);
                    }

                    ghostInfo.Components[comp].fields[field].Field = fieldInfo;

                    if (ghostInfo.DefaultClientInstantiationType ==
                        GhostAuthoringComponent.ClientInstantionType.OwnerPredicted &&
                        ghostInfo.PredictingPlayerNetworkId == ghostInfo.Components[comp].name + "." +
                        ghostInfo.Components[comp].fields[field].name)
                    {
                        ownerField = GetShortName(ghostInfo.Components[comp]) +
                                     ghostInfo.Components[comp].fields[field].name;
                    }
                }
            }

            var assetPath = GhostCodeGen.GetPrefabAssetPath(ghostInfo.gameObject);

            var batch = new GhostCodeGen.Batch();

            GenerateSnapshotData(ghostInfo, ownerField, assetPath, batch);
            GenerateSerializer(ghostInfo, assetPath, batch);
            GenerateUpdateSystem(ghostInfo, ownerField, assetPath, batch);
            var didWrite = batch.Flush(testOnly);

            AssetDatabase.Refresh();
            return(didWrite ? GhostCodeGen.Status.Ok : GhostCodeGen.Status.NotModified);
        }