private async Task <int> /*int*/ RunBulkEditInternal(BulkEditOptions opts) { List <string> files = MainController.Get().ActiveMod.Files; foreach (var path in files) { var fullpath = Path.Combine(MainController.Get().ActiveMod.FileDirectory, path); if (opts.ext != null && !Path.GetExtension(fullpath).Contains(opts.ext)) { continue; } CR2WFile cr2w; using (var fs = new FileStream(fullpath, FileMode.Open, FileAccess.Read)) using (var reader = new BinaryReader(fs)) { cr2w = new CR2WFile(reader); fs.Close(); } //EditVariablesInFile(path, cr2w, chunk, var, type, val); await Task.Run(() => EditVariablesInFile(path, cr2w, opts)) .ContinueWith(antecedent => { using (var fs = new FileStream($"{fullpath}", FileMode.Create, FileAccess.ReadWrite)) using (var writer = new BinaryWriter(fs)) { cr2w.Write(writer); } }); } return(0); }
private void EditVariablesInFile(string path, CR2WFile file, BulkEditOptions opts ) { if (file == null) { return; } if (opts.chunk != null && !file.chunks.Any(_ => opts.chunk.Contains(_.Type))) { return; } // get chunks that match chunkname List <CR2WExportWrapper> chunks = opts.chunk != null?file.chunks.Where(_ => opts.chunk.Contains(_.Type)).ToList() : file.chunks; foreach (CR2WExportWrapper c in chunks) { EditVariables(c.data); } AddTextStatic($"Finished {path}.\r\n", Logtype.Success); // local void EditVariables(CVariable vec) { if (vec == null) { return; } TryEditVariable(vec, vec.Name); //check children FieldInfo[] fields = vec.GetType().GetFields(); foreach (var f in fields) { // exclude the cr2w parent variable if (f.Name == "cr2w") { continue; } // edit lists var v = f.GetValue(vec); if (v is IList && v.GetType().IsGenericType) { foreach (var listitem in (v as IList)) { if (listitem is CVariable) { EditVariables(listitem as CVariable); } } } if (v is CVariable variable) { if (!TryEditVariable(v as CVariable, vec.Name)) { EditVariables(variable); // check if variable has more children } } } } bool TryEditVariable(CVariable v, string parentname) { if (v == null) { return(false); } // is a match for name if (v.Name == opts.var) { // is not of type if (opts.type != null && v.Type != opts.type) { return(false); } // exclude values if (opts.exc != null && opts.exc.Count() > 0 && opts.exc.Contains(v.ToString())) { return(false); } if (opts.inc != null && opts.inc.Count() > 0 && !opts.inc.Contains(v.ToString())) { return(false); } // edit value try { // check option switch (opts.option) { case "*": { switch (opts.type) { case "Uint64": ((CUInt64)v).val *= ulong.Parse(opts.val); AddTextStatic($"Succesfully edited a variable in {parentname}: {path}.\r\n", Logtype.Normal); break; case "Int64": ((CInt64)v).val *= long.Parse(opts.val); AddTextStatic($"Succesfully edited a variable in {parentname}: {path}.\r\n", Logtype.Normal); break; case "Uint32": ((CUInt32)v).val *= uint.Parse(opts.val); AddTextStatic($"Succesfully edited a variable in {parentname}: {path}.\r\n", Logtype.Normal); break; case "Int32": ((CInt32)v).val *= int.Parse(opts.val); AddTextStatic($"Succesfully edited a variable in {parentname}: {path}.\r\n", Logtype.Normal); break; case "Uint16": ((CUInt16)v).val *= ushort.Parse(opts.val); AddTextStatic($"Succesfully edited a variable in {parentname}: {path}.\r\n", Logtype.Normal); break; case "Int16": ((CInt16)v).val *= short.Parse(opts.val); AddTextStatic($"Succesfully edited a variable in {parentname}: {path}.\r\n", Logtype.Normal); break; case "Uint8": ((CUInt8)v).val *= byte.Parse(opts.val); AddTextStatic($"Succesfully edited a variable in {parentname}: {path}.\r\n", Logtype.Normal); break; case "Int8": ((CInt8)v).val *= sbyte.Parse(opts.val); AddTextStatic($"Succesfully edited a variable in {parentname}: {path}.\r\n", Logtype.Normal); break; default: break; } break; } case "/": { switch (opts.type) { case "Uint64": ((CUInt64)v).val /= ulong.Parse(opts.val); AddTextStatic($"Succesfully edited a variable in {parentname}: {path}.\r\n", Logtype.Normal); break; case "Int64": ((CInt64)v).val /= long.Parse(opts.val); AddTextStatic($"Succesfully edited a variable in {parentname}: {path}.\r\n", Logtype.Normal); break; case "Uint32": ((CUInt32)v).val /= uint.Parse(opts.val); AddTextStatic($"Succesfully edited a variable in {parentname}: {path}.\r\n", Logtype.Normal); break; case "Int32": ((CInt32)v).val /= int.Parse(opts.val); AddTextStatic($"Succesfully edited a variable in {parentname}: {path}.\r\n", Logtype.Normal); break; case "Uint16": ((CUInt16)v).val /= ushort.Parse(opts.val); AddTextStatic($"Succesfully edited a variable in {parentname}: {path}.\r\n", Logtype.Normal); break; case "Int16": ((CInt16)v).val /= short.Parse(opts.val); AddTextStatic($"Succesfully edited a variable in {parentname}: {path}.\r\n", Logtype.Normal); break; case "Uint8": ((CUInt8)v).val /= byte.Parse(opts.val); AddTextStatic($"Succesfully edited a variable in {parentname}: {path}.\r\n", Logtype.Normal); break; case "Int8": ((CInt8)v).val /= sbyte.Parse(opts.val); AddTextStatic($"Succesfully edited a variable in {parentname}: {path}.\r\n", Logtype.Normal); break; default: break; } break; } case "+": { switch (opts.type) { case "Uint64": ((CUInt64)v).val += ulong.Parse(opts.val); AddTextStatic($"Succesfully edited a variable in {parentname}: {path}.\r\n", Logtype.Normal); break; case "Int64": ((CInt64)v).val += long.Parse(opts.val); AddTextStatic($"Succesfully edited a variable in {parentname}: {path}.\r\n", Logtype.Normal); break; case "Uint32": ((CUInt32)v).val += uint.Parse(opts.val); AddTextStatic($"Succesfully edited a variable in {parentname}: {path}.\r\n", Logtype.Normal); break; case "Int32": ((CInt32)v).val += int.Parse(opts.val); AddTextStatic($"Succesfully edited a variable in {parentname}: {path}.\r\n", Logtype.Normal); break; case "Uint16": ((CUInt16)v).val += ushort.Parse(opts.val); AddTextStatic($"Succesfully edited a variable in {parentname}: {path}.\r\n", Logtype.Normal); break; case "Int16": ((CInt16)v).val += short.Parse(opts.val); AddTextStatic($"Succesfully edited a variable in {parentname}: {path}.\r\n", Logtype.Normal); break; case "Uint8": ((CUInt8)v).val += byte.Parse(opts.val); AddTextStatic($"Succesfully edited a variable in {parentname}: {path}.\r\n", Logtype.Normal); break; case "Int8": ((CInt8)v).val += sbyte.Parse(opts.val); AddTextStatic($"Succesfully edited a variable in {parentname}: {path}.\r\n", Logtype.Normal); break; case "String": ((CString)v).val += opts.val; break; default: break; } break; } case "-": { switch (opts.type) { case "Uint64": ((CUInt64)v).val -= ulong.Parse(opts.val); break; case "Int64": ((CInt64)v).val -= long.Parse(opts.val); break; case "Uint32": ((CUInt32)v).val -= uint.Parse(opts.val); break; case "Int32": ((CInt32)v).val -= int.Parse(opts.val); break; case "Uint16": ((CUInt16)v).val -= ushort.Parse(opts.val); break; case "Int16": ((CInt16)v).val -= short.Parse(opts.val); break; case "Uint8": ((CUInt8)v).val -= byte.Parse(opts.val); break; case "Int8": ((CInt8)v).val -= sbyte.Parse(opts.val); break; default: break; } break; } default: v.SetValue(opts.val); AddTextStatic($"Succesfully edited a variable in {parentname}: {path}.\r\n", Logtype.Normal); break; } return(true); } catch (Exception ex) { bool errors; bool.TryParse(opts.err, out errors); if (errors) { AddTextStatic($"Some parsing error: {ex.Message}.\r\n", Logtype.Error); } return(false); } } return(false); } }
private async Task <int> /*int*/ RunBulkEdit(ConsoleBulkEditOptions opts) { var vm = MockKernel.Get().GetBulkEditorViewModel(); // Bool, Uint64, Int64, Uint32, Int32, Uint16, Int16, Uint8, Int8 BulkEditOptions.AvailableTypes type = BulkEditOptions.AvailableTypes.ANY; if (opts.type == "Bool") { type = BulkEditOptions.AvailableTypes.CBool; } else if (opts.type == "Uint64") { type = BulkEditOptions.AvailableTypes.CUInt64; } else if (opts.type == "Int64") { type = BulkEditOptions.AvailableTypes.CInt64; } else if (opts.type == "Uint32") { type = BulkEditOptions.AvailableTypes.CUInt32; } else if (opts.type == "Int32") { type = BulkEditOptions.AvailableTypes.CInt32; } else if (opts.type == "Uint16") { type = BulkEditOptions.AvailableTypes.CUInt16; } else if (opts.type == "Int16") { type = BulkEditOptions.AvailableTypes.CInt16; } else if (opts.type == "Uint8") { type = BulkEditOptions.AvailableTypes.CUInt8; } else if (opts.type == "Int8") { type = BulkEditOptions.AvailableTypes.CInt8; } else if (opts.type == "String") { type = BulkEditOptions.AvailableTypes.CString; } var operation = BulkEditOptions.AvailableOperations.Replace; if (opts.option == "*") { operation = BulkEditOptions.AvailableOperations.Multiply; } if (opts.option == "/") { operation = BulkEditOptions.AvailableOperations.Divide; } if (opts.option == "+") { operation = BulkEditOptions.AvailableOperations.Add; } if (opts.option == "-") { operation = BulkEditOptions.AvailableOperations.Subtract; } var _opts = new BulkEditOptions() { Name = opts.varname, Value = opts.val, ChunkName = opts.chunk, Type = type, Extension = opts.ext, Operation = operation }; if (opts.exc.Count() != 0) { _opts.Exclude = string.Join(",", opts.exc.ToArray()); } if (opts.inc.Count() != 0) { _opts.Include = string.Join(",", opts.inc.ToArray()); } return(await Task.Run(() => vm.RunBulkEditInternal(_opts))); }
private async Task <int> /*int*/ RunBulkEdit(BulkEditOptions opts) { return(await Task.Run(() => RunBulkEditInternal(opts))); //return RunBulkEdit(opts.ext, opts.chunk, opts.var, opts.type, opts.val); }
private async Task <int> RunBulkEdit(BulkEditOptions opts) { return(await Task.Run(() => RunBulkEdit(opts.ext, opts.chunk, opts.var, opts.type, opts.val))); }