private static CustomVariable[] GetNetworkVariables(EntitySave entitySave) { return(entitySave.CustomVariables .Where(item => NetworkEntityViewModel.IsNetworked(item)) .ToArray()); }
private void HandleGluxLoaded() { TaskManager.Self.AddSync(() => { var areAnyNetworked = false; foreach (var screen in GlueState.Self.CurrentGlueProject.Screens) { if (NetworkScreenViewModel.IsNetworked(screen)) { areAnyNetworked = true; NetworkScreenCodeGenerator.GenerateCodeFor(screen, save: false); } } foreach (var entity in GlueState.Self.CurrentGlueProject.Entities) { if (NetworkEntityViewModel.IsNetworked(entity)) { areAnyNetworked = true; NetworkEntityCodeGenerator.GenerateCodeFor(entity, save: false); } } if (areAnyNetworked) { NetworkConfigurationCodeGenerator.GenerateConfiguration(); GlueCommands.Self.ProjectCommands.SaveProjects(); } }, "Regenerating network files"); }
private static string GenerateConfigurationCode() { ICodeBlock topBlock = new CodeBlockBaseNoIndent(null); var fileName = GlueState.Self.CurrentGlueProjectFileName; var projectName = FileManager.RemoveExtension(FileManager.RemovePath(fileName)); ICodeBlock codeBlock = topBlock.Namespace(GlueState.Self.ProjectNamespace); codeBlock = codeBlock.Class("", "GameNetworkConfiguration : RedGrin.NetworkConfiguration"); var constructor = codeBlock.Constructor("public", "GameNetworkConfiguration", ""); var portNumber = projectName.GetHashCode() % (ushort.MaxValue - 1024) + 1024; constructor.Line($"ApplicationName = \"{projectName}\";"); constructor.Line($"ApplicationPort = {portNumber};"); constructor.Line($"DeadReckonSeconds = 1.0f;"); constructor.Line($"EntityStateTypes = new System.Collections.Generic.List<System.Type>();"); var netEntities = GlueState.Self.CurrentGlueProject.Entities .Where(item => NetworkEntityViewModel.IsNetworked(item)); foreach (var netEntity in netEntities) { var netStateFullName = CodeGeneratorCommonLogic.GetNetStateFullName(netEntity); constructor.Line( $"EntityStateTypes.Add(typeof({netStateFullName}));"); } return(topBlock.ToString()); }
private void HandleEntityRemoved(EntitySave entity, List <string> filesToRemove) { if (NetworkEntityViewModel.IsNetworked(entity)) { var filePaths = CodeGeneratorCommonLogic.GetAllNetworkFilesFor(entity); filesToRemove.AddRange(filePaths.Select(item => item.FullPath)); // All screens no longer need to handle creating this: TaskManager.Self.AddSync(NetworkScreenCodeGenerator.GenerateAllNetworkScreenCode, "Regenerating network screens due to entity removal"); } }
private void HandleVariableRemoved(CustomVariable variable) { var currentEntity = GlueState.Self.CurrentEntitySave; if (currentEntity != null && NetworkEntityViewModel.IsNetworked(currentEntity) && NetworkEntityViewModel.IsNetworked(variable) ) { TaskManager.Self.AddSync(() => NetworkEntityCodeGenerator.GenerateCodeFor(currentEntity), "Regenerating networked due to variable removed"); } }
private void HandlePropertyChanged(string changedMember, object oldValue) { if (changedMember == nameof(EntitySave.CreatedByOtherEntities) && GlueState.Self.CurrentCustomVariable == null) { var entity = GlueState.Self.CurrentEntitySave; var isNetworkEntity = NetworkEntityViewModel.IsNetworked(entity); if (isNetworkEntity) { TaskManager.Self.AddSync(NetworkScreenCodeGenerator.GenerateAllNetworkScreenCode, "Generating all networked screens because of an entity change"); } } }
private void HandleEntityViewModelPropertyChanged(object sender, PropertyChangedEventArgs e) { var currentEntity = GlueState.Self.CurrentEntitySave; if (currentEntity != null && entityViewModel != null) { TaskManager.Self.AddSync(() => { var createdNewVariable = entityViewModel.ApplyTo(currentEntity); if (createdNewVariable) { GlueCommands.Self.RefreshCommands.RefreshUi(currentEntity); } GlueCommands.Self.GluxCommands.SaveGluxTask(); NetworkEntityCodeGenerator.GenerateCodeFor(currentEntity); // Whenever a new entity is modified, we may need to re-generate all screens because screens // have a switch statement for creating entities based on networked entities: var changedIsNetworked = e.PropertyName == nameof(NetworkEntityViewModel.IsNetworkEntity); if (changedIsNetworked) { if (NetworkEntityViewModel.IsNetworked(currentEntity) == false) { // set this to not be networked, need to remove the files: var networkedFiles = CodeGeneratorCommonLogic.GetAllNetworkFilesFor(currentEntity); foreach (var file in networkedFiles) { CodeGeneratorCommonLogic.RemoveCodeFileFromProject(file); } } else { NetworkConfigurationCodeGenerator.GenerateConfiguration(); MessagesCodeGenerator.GenerateAllMessages(); } NetworkScreenCodeGenerator.GenerateAllNetworkScreenCode(); } }, "Reacting to networked entity view model change"); } }
public static void GenerateCodeFor(EntitySave entitySave, bool save = true) { var isNetworkEntity = NetworkEntityViewModel.IsNetworked(entitySave); if (isNetworkEntity) { var entityGeneratedCode = GetGeneratedEntityNetworkCode(entitySave); var generatedEntityNetworkFilePath = CodeGeneratorCommonLogic.GetGeneratedElementNetworkFilePathFor(entitySave); CodeGeneratorCommonLogic.SaveFile(entityGeneratedCode, generatedEntityNetworkFilePath); CodeGeneratorCommonLogic.AddCodeFileToProject(generatedEntityNetworkFilePath); var netStateGeneratedCode = GenerateNetStateGeneratedCode(entitySave); var generatedNetStateFilePath = CodeGeneratorCommonLogic.GetGeneratedNetStateFilePathFor(entitySave); CodeGeneratorCommonLogic.SaveFile(netStateGeneratedCode, generatedNetStateFilePath); CodeGeneratorCommonLogic.AddCodeFileToProject(generatedNetStateFilePath); var customNetStateFilePath = CodeGeneratorCommonLogic.GetCustomNetStateFilePathFor(entitySave); if (customNetStateFilePath.Exists() == false) { var customNetStateCode = GenerateEmptyCustomNetStateCode(entitySave); CodeGeneratorCommonLogic.SaveFile(customNetStateCode, customNetStateFilePath); } CodeGeneratorCommonLogic.AddCodeFileToProject(customNetStateFilePath); var customEntityNetworkFilePath = CodeGeneratorCommonLogic.GetCustomElementNetworkFilePathFor(entitySave); if (customEntityNetworkFilePath.Exists() == false) { var customEntityNetworkCode = GenerateEmptyCustomEntityNetworkCode(entitySave); CodeGeneratorCommonLogic.SaveFile(customEntityNetworkCode, customEntityNetworkFilePath); } CodeGeneratorCommonLogic.AddCodeFileToProject(customEntityNetworkFilePath); GlueCommands.Self.ProjectCommands.MakeGeneratedCodeItemsNested(); if (save) { GlueCommands.Self.ProjectCommands.SaveProjects(); } } }
private static void GenerateReceiveClaimEntity(ICodeBlock codeBlock, ScreenSave screen) { var function = codeBlock.Function("private void", "HandleClaimEntity", $"{GlueState.Self.ProjectNamespace}.Messages.ClaimEntity claim"); var switchBlock = function.Switch("claim.EntityName"); foreach (var instance in screen.AllNamedObjects) { var entity = instance.GetReferencedElement() as EntitySave; if (entity != null && NetworkEntityViewModel.IsNetworked(entity)) { var caseBlock = switchBlock.Case($"\"{instance.FieldName}\""); caseBlock.Line($"{instance.FieldName}.OwnerId = claim.OwnerId;"); caseBlock.Line($"{instance.FieldName}.EntityId = claim.EntityId;"); // break is automatically added } } }
private static void GenerateRequestCreateEntity(ICodeBlock codeBlock) { var requestCreateMethod = codeBlock.Function( "public RedGrin.INetworkEntity", "RequestCreateEntity", "long ownerId, object entityData"); requestCreateMethod.Line("RedGrin.INetworkEntity entity = null;"); bool needsElseIf = false; var netEntities = GlueState.Self.CurrentGlueProject.Entities .Where(item => NetworkEntityViewModel.IsNetworked(item)); foreach (var entitySave in netEntities) { ICodeBlock ifBlock; var fullNetStateType = CodeGeneratorCommonLogic.GetNetStateFullName(entitySave); var fullEntityType = CodeGeneratorCommonLogic.GetElementFullName(entitySave); string ifcontents = $"entityData is {fullNetStateType}"; if (needsElseIf == false) { ifBlock = requestCreateMethod.If(ifcontents); } else { ifBlock = requestCreateMethod.ElseIf(ifcontents); } var hasFactory = entitySave.CreatedByOtherEntities; if (hasFactory) { var factoryName = $"{GlueState.Self.ProjectNamespace}.Factories.{entitySave.GetStrippedName()}Factory"; ifBlock.Line($"entity = {factoryName}.CreateNew();"); } else { ifBlock.Line($"entity = new {fullEntityType}();"); } // Even though the NetworkManager assigns the owner ID, we're going to do it here // to before calling UpdateFromState, so that any custom code that gets triggered from // UpdateFromState are guaranteed to have the right ID: ifBlock.Line("entity.OwnerId = ownerId;"); ifBlock.Line("entity.UpdateFromState(entityData, 0);"); needsElseIf = true; } // At first I thought to have the CustomRequestCreateNetworkEntity // inside the if/else if so that the created entity could be modified // but it's possible the user may want to have their own totally custom // network entities and network entity states, in which case they will need // to instantiate the object fully in custom code. Therefore, we'll call the // method no matter what, even if the entity is null requestCreateMethod.Line("CustomRequestCreateNetworkEntity(ref entity, entityData);"); requestCreateMethod.Line("return entity;"); }