feat: Geutebruck GeViScope/GeViSoft Action Mapping System - MVP

This MVP release provides a complete full-stack solution for managing action mappings
in Geutebruck's GeViScope and GeViSoft video surveillance systems.

## Features

### Flutter Web Application (Port 8081)
- Modern, responsive UI for managing action mappings
- Action picker dialog with full parameter configuration
- Support for both GSC (GeViScope) and G-Core server actions
- Consistent UI for input and output actions with edit/delete capabilities
- Real-time action mapping creation, editing, and deletion
- Server categorization (GSC: prefix for GeViScope, G-Core: prefix for G-Core servers)

### FastAPI REST Backend (Port 8000)
- RESTful API for action mapping CRUD operations
- Action template service with comprehensive action catalog (247 actions)
- Server management (G-Core and GeViScope servers)
- Configuration tree reading and writing
- JWT authentication with role-based access control
- PostgreSQL database integration

### C# SDK Bridge (gRPC, Port 50051)
- Native integration with GeViSoft SDK (GeViProcAPINET_4_0.dll)
- Action mapping creation with correct binary format
- Support for GSC and G-Core action types
- Proper Camera parameter inclusion in action strings (fixes CrossSwitch bug)
- Action ID lookup table with server-specific action IDs
- Configuration reading/writing via SetupClient

## Bug Fixes
- **CrossSwitch Bug**: GSC and G-Core actions now correctly display camera/PTZ head parameters in GeViSet
- Action strings now include Camera parameter: `@ PanLeft (Comment: "", Camera: 101028)`
- Proper filter flags and VideoInput=0 for action mappings
- Correct action ID assignment (4198 for GSC, 9294 for G-Core PanLeft)

## Technical Stack
- **Frontend**: Flutter Web, Dart, Dio HTTP client
- **Backend**: Python FastAPI, PostgreSQL, Redis
- **SDK Bridge**: C# .NET 8.0, gRPC, GeViSoft SDK
- **Authentication**: JWT tokens
- **Configuration**: GeViSoft .set files (binary format)

## Credentials
- GeViSoft/GeViScope: username=sysadmin, password=masterkey
- Default admin: username=admin, password=admin123

## Deployment
All services run on localhost:
- Flutter Web: http://localhost:8081
- FastAPI: http://localhost:8000
- SDK Bridge gRPC: localhost:50051
- GeViServer: localhost (default port)

Generated with Claude Code (https://claude.com/claude-code)

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
Administrator
2025-12-31 18:10:54 +01:00
commit 14893e62a5
4189 changed files with 1395076 additions and 0 deletions

View File

@@ -0,0 +1,404 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace GeViSetEditor.Core.Parsers
{
/// <summary>
/// Advanced binary analyzer to understand .set file structure completely
/// </summary>
public class AdvancedBinaryAnalyzer
{
private byte[] _data;
private int _position;
/// <summary>
/// Analyze binary structure and report findings
/// </summary>
public BinaryStructureReport Analyze(byte[] data)
{
_data = data;
_position = 0;
var report = new BinaryStructureReport
{
FileSize = data.Length
};
Console.WriteLine("\n=== Advanced Binary Structure Analysis ===\n");
// Read header
AnalyzeHeader(report);
// Find all markers
FindAllMarkers(report);
// Analyze data types distribution
AnalyzeDataTypes(report);
// Find repeated patterns
FindPatterns(report);
// Identify section structure
AnalyzeSections(report);
return report;
}
private void AnalyzeHeader(BinaryStructureReport report)
{
Console.WriteLine("=== Header Analysis ===");
// Skip null byte if present
if (_position < _data.Length && _data[_position] == 0x00)
{
report.HeaderHasNullPrefix = true;
_position++;
Console.WriteLine($"Null byte at position 0");
}
// Read header length
if (_position < _data.Length)
{
byte headerLen = _data[_position++];
if (_position + headerLen <= _data.Length)
{
report.Header = Encoding.UTF8.GetString(_data, _position, headerLen);
_position += headerLen;
Console.WriteLine($"Header: '{report.Header}' (length: {headerLen})");
Console.WriteLine($"Header ends at offset: {_position} (0x{_position:X})\n");
report.HeaderEndOffset = _position;
}
}
}
private void FindAllMarkers(BinaryStructureReport report)
{
Console.WriteLine("=== Finding Known Markers ===");
// Known markers
var markers = new Dictionary<string, byte[]>
{
{ "Rules", new byte[] { 0x05, 0x52, 0x75, 0x6C, 0x65, 0x73 } },
{ "Description", new byte[] { 0x07, 0x0B, 0x44, 0x65, 0x73, 0x63, 0x72, 0x69, 0x70, 0x74, 0x69, 0x6F, 0x6E } },
{ "Name", new byte[] { 0x07, 0x04, 0x4E, 0x61, 0x6D, 0x65 } },
{ "Enabled", new byte[] { 0x07, 0x07, 0x45, 0x6E, 0x61, 0x62, 0x6C, 0x65, 0x64 } }
};
foreach (var marker in markers)
{
var offsets = FindPattern(_data, marker.Value);
report.Markers[marker.Key] = offsets;
Console.WriteLine($"{marker.Key}: {offsets.Count} occurrences");
}
Console.WriteLine();
}
private void AnalyzeDataTypes(BinaryStructureReport report)
{
Console.WriteLine("=== Data Type Distribution ===");
int boolCount = 0, intCount = 0, stringCount = 0, actionCount = 0;
for (int i = 0; i < _data.Length; i++)
{
if (_data[i] == 0x01) boolCount++;
else if (_data[i] == 0x04) intCount++;
else if (_data[i] == 0x07) stringCount++;
// Action string pattern
if (i + 2 < _data.Length &&
_data[i] == 0x07 && _data[i+1] == 0x01 && _data[i+2] == 0x40)
{
actionCount++;
}
}
Console.WriteLine($"0x01 (Boolean marker): {boolCount}");
Console.WriteLine($"0x04 (Integer marker): {intCount}");
Console.WriteLine($"0x07 (String marker): {stringCount}");
Console.WriteLine($"Action pattern (07 01 40): {actionCount}\n");
report.DataTypeCounts["boolean"] = boolCount;
report.DataTypeCounts["integer"] = intCount;
report.DataTypeCounts["string"] = stringCount;
report.DataTypeCounts["action"] = actionCount;
}
private void FindPatterns(BinaryStructureReport report)
{
Console.WriteLine("=== Repeated Byte Patterns ===");
// Find sequences that repeat
var sequences = new Dictionary<string, int>();
// Check 4-byte patterns
for (int i = 0; i < _data.Length - 4; i++)
{
var seq = $"{_data[i]:X2} {_data[i+1]:X2} {_data[i+2]:X2} {_data[i+3]:X2}";
if (sequences.ContainsKey(seq))
sequences[seq]++;
else
sequences[seq] = 1;
}
var topPatterns = sequences
.Where(kv => kv.Value > 50) // More than 50 occurrences
.OrderByDescending(kv => kv.Value)
.Take(10);
foreach (var pattern in topPatterns)
{
Console.WriteLine($"{pattern.Key}: {pattern.Value} times");
}
Console.WriteLine();
}
private void AnalyzeSections(BinaryStructureReport report)
{
Console.WriteLine("=== Section Structure Analysis ===");
// Start after header
_position = report.HeaderEndOffset;
int sectionCount = 0;
while (_position < _data.Length - 100)
{
var section = TryParseSection(_position);
if (section != null)
{
report.Sections.Add(section);
sectionCount++;
_position = section.EndOffset;
}
else
{
_position++;
}
}
Console.WriteLine($"Identified {sectionCount} potential sections\n");
// Show first few sections
Console.WriteLine("=== First 10 Sections ===");
foreach (var section in report.Sections.Take(10))
{
Console.WriteLine($"Offset {section.StartOffset}-{section.EndOffset}: {section.Name}");
Console.WriteLine($" Items: {section.Items.Count}, Size: {section.Size} bytes");
}
Console.WriteLine();
}
private SectionInfo TryParseSection(int offset)
{
// Try to identify section start
// Common pattern: Pascal string followed by items
if (offset + 10 > _data.Length)
return null;
// Look for string marker
if (_data[offset] != 0x07)
return null;
byte nameLen = _data[offset + 1];
if (nameLen < 3 || nameLen > 50 || offset + 2 + nameLen > _data.Length)
return null;
string name = Encoding.UTF8.GetString(_data, offset + 2, nameLen);
// Validate name
if (!name.All(c => char.IsLetterOrDigit(c) || c == '_' || c == ' '))
return null;
var section = new SectionInfo
{
StartOffset = offset,
Name = name,
NameLength = nameLen
};
int pos = offset + 2 + nameLen;
// Parse items in section until we hit another section or Rules
int itemCount = 0;
while (pos < _data.Length - 10 && itemCount < 100)
{
var item = TryParseItem(pos);
if (item != null)
{
section.Items.Add(item);
pos = item.EndOffset;
itemCount++;
}
else
{
// Check if we hit next section or Rules
if (IsLikelyNextSection(pos))
break;
pos++;
}
}
section.EndOffset = pos;
section.Size = pos - offset;
return section.Items.Count > 0 ? section : null;
}
private ItemInfo TryParseItem(int offset)
{
if (offset + 5 > _data.Length)
return null;
// Item pattern: 07 <len> <key> <type> <value>
if (_data[offset] != 0x07)
return null;
byte keyLen = _data[offset + 1];
if (keyLen < 2 || keyLen > 40 || offset + 2 + keyLen + 2 > _data.Length)
return null;
string key = Encoding.UTF8.GetString(_data, offset + 2, keyLen);
// Validate key
if (!key.All(c => char.IsLetterOrDigit(c) || c == '_' || c == '-'))
return null;
int valueOffset = offset + 2 + keyLen;
byte typeMarker = _data[valueOffset];
var item = new ItemInfo
{
StartOffset = offset,
Key = key,
TypeMarker = typeMarker
};
// Parse value based on type
switch (typeMarker)
{
case 0x01: // Boolean
if (valueOffset + 2 <= _data.Length)
{
item.Value = _data[valueOffset + 1] != 0;
item.ValueType = "boolean";
item.EndOffset = valueOffset + 2;
return item;
}
break;
case 0x04: // Integer
if (valueOffset + 5 <= _data.Length)
{
item.Value = BitConverter.ToInt32(_data, valueOffset + 1);
item.ValueType = "integer";
item.EndOffset = valueOffset + 5;
return item;
}
break;
case 0x07: // String
if (valueOffset + 2 <= _data.Length)
{
byte strLen = _data[valueOffset + 1];
if (strLen > 0 && valueOffset + 2 + strLen <= _data.Length)
{
item.Value = Encoding.UTF8.GetString(_data, valueOffset + 2, strLen);
item.ValueType = "string";
item.EndOffset = valueOffset + 2 + strLen;
return item;
}
}
break;
}
return null;
}
private bool IsLikelyNextSection(int pos)
{
if (pos + 10 > _data.Length)
return false;
// Check for Rules marker
if (_data[pos] == 0x05 && pos + 6 <= _data.Length)
{
string marker = Encoding.UTF8.GetString(_data, pos + 1, 5);
if (marker == "Rules")
return true;
}
// Check for section name pattern
if (_data[pos] == 0x07)
{
byte len = _data[pos + 1];
if (len >= 5 && len <= 30 && pos + 2 + len <= _data.Length)
{
string name = Encoding.UTF8.GetString(_data, pos + 2, len);
return name.All(c => char.IsLetterOrDigit(c) || c == '_');
}
}
return false;
}
private List<int> FindPattern(byte[] data, byte[] pattern)
{
var offsets = new List<int>();
for (int i = 0; i <= data.Length - pattern.Length; i++)
{
bool match = true;
for (int j = 0; j < pattern.Length; j++)
{
if (data[i + j] != pattern[j])
{
match = false;
break;
}
}
if (match)
offsets.Add(i);
}
return offsets;
}
}
public class BinaryStructureReport
{
public int FileSize { get; set; }
public bool HeaderHasNullPrefix { get; set; }
public string Header { get; set; } = "";
public int HeaderEndOffset { get; set; }
public Dictionary<string, List<int>> Markers { get; set; } = new();
public Dictionary<string, int> DataTypeCounts { get; set; } = new();
public List<SectionInfo> Sections { get; set; } = new();
}
public class SectionInfo
{
public int StartOffset { get; set; }
public int EndOffset { get; set; }
public int Size { get; set; }
public string Name { get; set; } = "";
public int NameLength { get; set; }
public List<ItemInfo> Items { get; set; } = new();
}
public class ItemInfo
{
public int StartOffset { get; set; }
public int EndOffset { get; set; }
public string Key { get; set; } = "";
public byte TypeMarker { get; set; }
public string ValueType { get; set; } = "";
public object Value { get; set; }
}
}

View File

@@ -0,0 +1,209 @@
using System;
using System.IO;
using System.Text;
using System.Collections.Generic;
using GeViSetEditor.Core.Models;
namespace GeViSetEditor.Core.Parsers
{
/// <summary>
/// Writes ComprehensiveConfigFile back to binary .set format
/// Enables edit/add/delete operations on configuration
/// </summary>
public class BinaryConfigWriter
{
/// <summary>
/// Rebuild complete .set file from configuration nodes
/// </summary>
public byte[] Write(ComprehensiveConfigFile config)
{
using var ms = new MemoryStream();
// Write header
WriteHeader(ms, config);
// Write all nodes sequentially
foreach (var node in config.RootNodes)
{
WriteNode(ms, node);
}
return ms.ToArray();
}
private void WriteHeader(MemoryStream ms, ComprehensiveConfigFile config)
{
// Write null prefix if it was present
if (config.HeaderNullPrefix)
{
ms.WriteByte(0x00);
}
// Write header string
if (!string.IsNullOrEmpty(config.Header))
{
byte[] headerBytes = Encoding.UTF8.GetBytes(config.Header);
ms.WriteByte((byte)headerBytes.Length);
ms.Write(headerBytes, 0, headerBytes.Length);
}
}
private void WriteNode(MemoryStream ms, ConfigNode node)
{
switch (node.NodeType)
{
case "boolean":
WriteBoolean(ms, node);
break;
case "integer":
WriteInteger(ms, node);
break;
case "string":
WriteString(ms, node);
break;
case "property":
WriteProperty(ms, node);
break;
case "marker":
WriteMarker(ms, node);
break;
default:
throw new InvalidOperationException($"Unknown node type: {node.NodeType}");
}
}
private void WriteBoolean(MemoryStream ms, ConfigNode node)
{
ms.WriteByte(0x01); // Boolean marker
bool value = Convert.ToBoolean(node.Value);
ms.WriteByte(value ? (byte)1 : (byte)0);
}
private void WriteInteger(MemoryStream ms, ConfigNode node)
{
ms.WriteByte(0x04); // Integer marker
int value = Convert.ToInt32(node.Value);
byte[] bytes = BitConverter.GetBytes(value);
ms.Write(bytes, 0, bytes.Length);
}
private void WriteString(MemoryStream ms, ConfigNode node)
{
ms.WriteByte(0x07); // String marker
string value = node.Value?.ToString() ?? "";
byte[] bytes = Encoding.UTF8.GetBytes(value);
if (bytes.Length > 255)
throw new InvalidOperationException($"String too long: {bytes.Length} bytes (max 255)");
ms.WriteByte((byte)bytes.Length);
ms.Write(bytes, 0, bytes.Length);
}
private void WriteProperty(MemoryStream ms, ConfigNode node)
{
// Write property name (07 <len> <name>)
ms.WriteByte(0x07);
byte[] nameBytes = Encoding.UTF8.GetBytes(node.Name ?? "");
if (nameBytes.Length > 255)
throw new InvalidOperationException($"Property name too long: {nameBytes.Length} bytes (max 255)");
ms.WriteByte((byte)nameBytes.Length);
ms.Write(nameBytes, 0, nameBytes.Length);
// Write property value based on ValueType
WriteTypedValue(ms, node.Value, node.ValueType);
}
private void WriteMarker(MemoryStream ms, ConfigNode node)
{
// Write marker (05 <len> <name>)
ms.WriteByte(0x05);
byte[] nameBytes = Encoding.UTF8.GetBytes(node.Name ?? "");
if (nameBytes.Length > 255)
throw new InvalidOperationException($"Marker name too long: {nameBytes.Length} bytes (max 255)");
ms.WriteByte((byte)nameBytes.Length);
ms.Write(nameBytes, 0, nameBytes.Length);
// If it's a Rules marker with actions, write them
if (node.Name == "Rules" && node.Value is List<string> actions)
{
WriteRulesActions(ms, actions);
}
}
private void WriteRulesActions(MemoryStream ms, List<string> actions)
{
// Write action strings in the special format: 07 01 40 <len_2bytes_LE> <data>
foreach (var action in actions)
{
ms.WriteByte(0x07);
ms.WriteByte(0x01);
ms.WriteByte(0x40);
byte[] actionBytes = Encoding.UTF8.GetBytes(action);
ushort length = (ushort)actionBytes.Length;
byte[] lenBytes = BitConverter.GetBytes(length);
ms.Write(lenBytes, 0, 2); // Write length as 2-byte LE
ms.Write(actionBytes, 0, actionBytes.Length);
}
}
private void WriteTypedValue(MemoryStream ms, object? value, string? valueType)
{
if (value == null)
{
// Write null as empty string
ms.WriteByte(0x07);
ms.WriteByte(0x00);
return;
}
switch (valueType)
{
case "boolean":
ms.WriteByte(0x01);
bool boolVal = Convert.ToBoolean(value);
ms.WriteByte(boolVal ? (byte)1 : (byte)0);
break;
case "integer":
ms.WriteByte(0x04);
int intVal = Convert.ToInt32(value);
byte[] intBytes = BitConverter.GetBytes(intVal);
ms.Write(intBytes, 0, intBytes.Length);
break;
case "string":
ms.WriteByte(0x07);
string strVal = value.ToString() ?? "";
byte[] strBytes = Encoding.UTF8.GetBytes(strVal);
if (strBytes.Length > 255)
throw new InvalidOperationException($"String value too long: {strBytes.Length} bytes (max 255)");
ms.WriteByte((byte)strBytes.Length);
ms.Write(strBytes, 0, strBytes.Length);
break;
default:
// Default to string representation
WriteTypedValue(ms, value.ToString(), "string");
break;
}
}
}
}

View File

@@ -0,0 +1,402 @@
using System;
using System.Collections.Generic;
using System.Text;
using GeViSetEditor.Core.Models;
namespace GeViSetEditor.Core.Parsers
{
/// <summary>
/// Complete binary parser that understands the entire .set file structure
/// Parses ALL sections, items, and rules for full JSON serialization
/// </summary>
public class CompleteBinaryParser
{
private byte[] _data;
private int _position;
private Dictionary<int, string> _debugLog = new();
public SetFileComplete Parse(byte[] data)
{
_data = data;
_position = 0;
var setFile = new SetFileComplete
{
FileSize = data.Length,
RawData = data // Keep original for verification
};
Console.WriteLine($"=== Complete Binary Parser ===");
Console.WriteLine($"File size: {data.Length:N0} bytes\n");
try
{
// Parse header
ParseHeader(setFile);
// Parse all sections
ParseAllSections(setFile);
Console.WriteLine($"\n=== Parsing Complete ===");
Console.WriteLine($"Sections parsed: {setFile.Sections.Count}");
Console.WriteLine($"Total items: {setFile.Sections.Sum(s => s.Items.Count)}");
Console.WriteLine($"Total rules: {setFile.Sections.Sum(s => s.Rules.Count)}");
return setFile;
}
catch (Exception ex)
{
Console.WriteLine($"\nERROR at position {_position} (0x{_position:X}): {ex.Message}");
throw;
}
}
private void ParseHeader(SetFileComplete setFile)
{
Console.WriteLine("Parsing header...");
// Optional null byte
if (_position < _data.Length && _data[_position] == 0x00)
{
setFile.HeaderNullByte = true;
_position++;
}
// Header is length-prefixed string (NOT Pascal string with 0x07)
if (_position + 2 > _data.Length)
throw new Exception("File too short for header");
byte headerLen = _data[_position++];
if (headerLen == 0 || headerLen > 50 || _position + headerLen > _data.Length)
throw new Exception($"Invalid header length: {headerLen}");
setFile.Header = Encoding.UTF8.GetString(_data, _position, headerLen);
_position += headerLen;
Console.WriteLine($" Header: '{setFile.Header}'");
Console.WriteLine($" Position after header: {_position} (0x{_position:X})\n");
}
private void ParseAllSections(SetFileComplete setFile)
{
Console.WriteLine("Parsing sections...\n");
int sectionCount = 0;
int maxIterations = 10000; // Safety limit
while (_position < _data.Length - 10 && maxIterations-- > 0)
{
int sectionStart = _position;
var section = TryParseSection();
if (section != null)
{
sectionCount++;
setFile.Sections.Add(section);
if (sectionCount <= 20 || section.Items.Count > 0 || section.Rules.Count > 0)
{
Console.WriteLine($"Section #{sectionCount}: '{section.Name}' " +
$"(offset {sectionStart}, {section.Items.Count} items, {section.Rules.Count} rules)");
}
}
else
{
// Skip unknown byte
_position++;
}
}
Console.WriteLine($"\nTotal sections parsed: {sectionCount}");
}
private SectionComplete TryParseSection()
{
int startPos = _position;
try
{
// Try to read section name (Pascal string: 0x07 <len> <data>)
string sectionName = TryReadPascalString();
if (string.IsNullOrEmpty(sectionName) || sectionName.Length > 100)
{
_position = startPos;
return null;
}
var section = new SectionComplete
{
Name = sectionName,
FileOffset = startPos
};
// Parse section contents
ParseSectionContents(section);
section.FileEndOffset = _position;
return section;
}
catch
{
_position = startPos;
return null;
}
}
private void ParseSectionContents(SectionComplete section)
{
int maxIterations = 1000;
bool foundRules = false;
while (_position < _data.Length - 10 && maxIterations-- > 0)
{
int itemStart = _position;
// Check for "Rules" marker (length-prefixed, no 0x07)
if (CheckForRulesMarker())
{
foundRules = true;
ParseRulesSubsection(section);
break; // Rules typically end a section
}
// Try to parse a config item
var item = TryParseConfigItem();
if (item != null)
{
section.Items.Add(item);
continue;
}
// Check if we've hit next section
if (IsLikelyNextSection())
{
break;
}
// Move forward
_position++;
}
}
private bool CheckForRulesMarker()
{
// Pattern: 05 52 75 6C 65 73 = "Rules" (length 5 + data)
if (_position + 6 > _data.Length)
return false;
return _data[_position] == 0x05 &&
_data[_position + 1] == 0x52 && // 'R'
_data[_position + 2] == 0x75 && // 'u'
_data[_position + 3] == 0x6C && // 'l'
_data[_position + 4] == 0x65 && // 'e'
_data[_position + 5] == 0x73; // 's'
}
private void ParseRulesSubsection(SectionComplete section)
{
// Skip "Rules" marker
_position += 6;
// Skip metadata bytes (counts, flags, etc.)
while (_position < _data.Length && _data[_position] <= 0x04)
{
_position++;
}
// Parse action rules
int ruleCount = 0;
int maxRules = 100;
int consecutiveFails = 0;
while (maxRules-- > 0 && _position < _data.Length - 20)
{
var rule = TryParseActionRule();
if (rule != null)
{
rule.RuleId = ruleCount++;
section.Rules.Add(rule);
consecutiveFails = 0;
}
else
{
_position++;
consecutiveFails++;
if (consecutiveFails > 100) break;
}
}
}
private ActionRuleComplete TryParseActionRule()
{
int startPos = _position;
try
{
var rule = new ActionRuleComplete
{
FileOffset = startPos
};
// Try to read action string (07 01 40 <len_2bytes> <data>)
string actionString = TryReadActionString();
if (!string.IsNullOrEmpty(actionString))
{
rule.Actions.Add(actionString);
return rule;
}
_position = startPos;
return null;
}
catch
{
_position = startPos;
return null;
}
}
private ConfigItemComplete TryParseConfigItem()
{
int startPos = _position;
try
{
// Read key (Pascal string)
string key = TryReadPascalString();
if (string.IsNullOrEmpty(key) || key.Length > 200)
{
_position = startPos;
return null;
}
// Read value (typed)
var (value, type) = TryReadTypedValue();
if (value == null)
{
_position = startPos;
return null;
}
return new ConfigItemComplete
{
Key = key,
Value = value,
ValueType = type,
FileOffset = startPos
};
}
catch
{
_position = startPos;
return null;
}
}
private string TryReadPascalString()
{
if (_position + 2 > _data.Length || _data[_position] != 0x07)
return null;
byte length = _data[_position + 1];
if (length == 0 || length > 200 || _position + 2 + length > _data.Length)
return null;
string result = Encoding.UTF8.GetString(_data, _position + 2, length);
_position += 2 + length;
return result;
}
private string TryReadActionString()
{
// Pattern: 07 01 40 <len_2bytes_LE> <data>
if (_position + 5 > _data.Length)
return null;
if (_data[_position] != 0x07 ||
_data[_position + 1] != 0x01 ||
_data[_position + 2] != 0x40)
return null;
int length = BitConverter.ToUInt16(_data, _position + 3);
if (length == 0 || length > 500 || _position + 5 + length > _data.Length)
return null;
string result = Encoding.UTF8.GetString(_data, _position + 5, length);
_position += 5 + length;
// Validate it looks like an action
if (string.IsNullOrWhiteSpace(result) || result.Length < 3)
return null;
return result;
}
private (object value, string type) TryReadTypedValue()
{
if (_position >= _data.Length)
return (null, null);
byte typeMarker = _data[_position];
switch (typeMarker)
{
case 0x01: // Boolean
if (_position + 2 <= _data.Length)
{
_position++;
bool value = _data[_position++] != 0;
return (value, "boolean");
}
break;
case 0x04: // Integer (4 bytes, little-endian)
if (_position + 5 <= _data.Length)
{
_position++;
int value = BitConverter.ToInt32(_data, _position);
_position += 4;
return (value, "integer");
}
break;
case 0x07: // String (Pascal string)
string value_str = TryReadPascalString();
if (value_str != null)
{
return (value_str, "string");
}
break;
}
return (null, null);
}
private bool IsLikelyNextSection()
{
// Check if next bytes look like a new section (Pascal string)
if (_position + 3 >= _data.Length)
return true;
if (_data[_position] != 0x07)
return false;
byte length = _data[_position + 1];
if (length < 3 || length > 50)
return false;
// Check if it's printable ASCII (likely a section name)
for (int i = 0; i < Math.Min(5, (int)length); i++)
{
if (_position + 2 + i >= _data.Length)
return false;
byte b = _data[_position + 2 + i];
if (b < 32 || b > 126)
return false;
}
return true;
}
}
}

View File

@@ -0,0 +1,333 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GeViSetEditor.Core.Models;
namespace GeViSetEditor.Core.Parsers
{
/// <summary>
/// Comprehensive parser that extracts ALL configuration data
/// Based on binary analysis showing 15K booleans, 9K integers, 6K strings
/// </summary>
public class ComprehensiveConfigParser
{
private byte[] _data;
private int _position;
private int _depth;
public ComprehensiveConfigFile Parse(byte[] data)
{
_data = data;
_position = 0;
_depth = 0;
var config = new ComprehensiveConfigFile
{
OriginalData = data,
FileSize = data.Length
};
Console.WriteLine($"\n=== Comprehensive Configuration Parser ===");
Console.WriteLine($"File size: {data.Length:N0} bytes\n");
// Parse header
ParseHeader(config);
Console.WriteLine($"Starting systematic parse from offset {_position}...\n");
// Parse all data structures systematically
int itemsParsed = 0;
while (_position < _data.Length - 10)
{
var node = TryParseNextNode();
if (node != null)
{
config.RootNodes.Add(node);
itemsParsed++;
if (itemsParsed % 1000 == 0)
Console.WriteLine($"Parsed {itemsParsed} nodes, position: {_position}/{_data.Length}");
}
else
{
// Skip unknown byte
_position++;
}
}
Console.WriteLine($"\n=== Parse Complete ===");
Console.WriteLine($"Total nodes parsed: {itemsParsed}");
Console.WriteLine($"Position: {_position}/{_data.Length}");
// Calculate statistics
CalculateStatistics(config);
return config;
}
private void ParseHeader(ComprehensiveConfigFile config)
{
// Skip null byte
if (_position < _data.Length && _data[_position] == 0x00)
{
config.HeaderNullPrefix = true;
_position++;
}
// Read header
if (_position < _data.Length)
{
byte headerLen = _data[_position++];
if (_position + headerLen <= _data.Length)
{
config.Header = Encoding.UTF8.GetString(_data, _position, headerLen);
_position += headerLen;
Console.WriteLine($"Header: '{config.Header}'");
Console.WriteLine($"Header ends at: {_position} (0x{_position:X})\n");
}
}
}
private ConfigNode TryParseNextNode()
{
if (_position >= _data.Length)
return null;
int startOffset = _position;
byte marker = _data[_position];
ConfigNode node = null;
switch (marker)
{
case 0x01: // Boolean
node = ParseBoolean(startOffset);
break;
case 0x04: // Integer
node = ParseInteger(startOffset);
break;
case 0x05: // Special marker (Rules, etc.)
node = ParseSpecialMarker(startOffset);
break;
case 0x07: // String or Object
node = ParseStringOrObject(startOffset);
break;
default:
// Unknown marker - might be raw data
return null;
}
return node;
}
private ConfigNode ParseBoolean(int startOffset)
{
if (_position + 2 > _data.Length)
return null;
_position++; // Skip 0x01
bool value = _data[_position++] != 0;
return new ConfigNode
{
StartOffset = startOffset,
EndOffset = _position,
NodeType = "boolean",
Value = value
};
}
private ConfigNode ParseInteger(int startOffset)
{
if (_position + 5 > _data.Length)
return null;
_position++; // Skip 0x04
int value = BitConverter.ToInt32(_data, _position);
_position += 4;
return new ConfigNode
{
StartOffset = startOffset,
EndOffset = _position,
NodeType = "integer",
Value = value
};
}
private ConfigNode ParseSpecialMarker(int startOffset)
{
if (_position + 2 > _data.Length)
return null;
_position++; // Skip 0x05
byte nameLen = _data[_position++];
if (nameLen > 100 || _position + nameLen > _data.Length)
{
_position = startOffset;
return null;
}
string markerName = Encoding.UTF8.GetString(_data, _position, nameLen);
_position += nameLen;
var node = new ConfigNode
{
StartOffset = startOffset,
EndOffset = _position,
NodeType = "marker",
Name = markerName
};
// If it's "Rules", parse action mappings
if (markerName == "Rules")
{
ParseRulesSection(node);
}
return node;
}
private ConfigNode ParseStringOrObject(int startOffset)
{
if (_position + 2 > _data.Length)
return null;
_position++; // Skip 0x07
byte length = _data[_position++];
if (length == 0 || length > 200 || _position + length > _data.Length)
{
_position = startOffset;
return null;
}
string stringValue = Encoding.UTF8.GetString(_data, _position, length);
_position += length;
// Check if this string is followed by typed values (object property)
if (_position < _data.Length)
{
byte nextMarker = _data[_position];
if (nextMarker == 0x01 || nextMarker == 0x04 || nextMarker == 0x07)
{
// This is a property name followed by value
var node = new ConfigNode
{
StartOffset = startOffset,
NodeType = "property",
Name = stringValue
};
// Parse the value
var valueNode = TryParseNextNode();
if (valueNode != null)
{
node.Value = valueNode.Value;
node.ValueType = valueNode.NodeType;
node.EndOffset = _position;
return node;
}
}
}
// Just a standalone string
return new ConfigNode
{
StartOffset = startOffset,
EndOffset = _position,
NodeType = "string",
Value = stringValue
};
}
private void ParseRulesSection(ConfigNode rulesNode)
{
// Skip metadata bytes
while (_position < _data.Length && _data[_position] <= 0x04)
{
_position++;
}
// Parse action strings (07 01 40 pattern)
var actions = new List<string>();
int attempts = 0;
while (attempts < 200 && _position + 10 < _data.Length)
{
if (_data[_position] == 0x07 &&
_data[_position + 1] == 0x01 &&
_data[_position + 2] == 0x40)
{
ushort actionLen = BitConverter.ToUInt16(_data, _position + 3);
if (actionLen > 0 && actionLen < 500 && _position + 5 + actionLen <= _data.Length)
{
string action = Encoding.UTF8.GetString(_data, _position + 5, actionLen);
actions.Add(action);
_position += 5 + actionLen;
attempts = 0; // Reset
}
else
{
_position++;
attempts++;
}
}
else
{
_position++;
attempts++;
}
}
rulesNode.Value = actions;
rulesNode.EndOffset = _position;
}
private void CalculateStatistics(ComprehensiveConfigFile config)
{
config.Statistics = new ComprehensiveConfigStatistics
{
TotalNodes = config.RootNodes.Count,
BooleanCount = config.RootNodes.Count(n => n.NodeType == "boolean"),
IntegerCount = config.RootNodes.Count(n => n.NodeType == "integer"),
StringCount = config.RootNodes.Count(n => n.NodeType == "string"),
PropertyCount = config.RootNodes.Count(n => n.NodeType == "property"),
MarkerCount = config.RootNodes.Count(n => n.NodeType == "marker"),
RulesCount = config.RootNodes.Count(n => n.NodeType == "marker" && n.Name == "Rules")
};
Console.WriteLine($"\n=== Statistics ===");
Console.WriteLine($"Total nodes: {config.Statistics.TotalNodes:N0}");
Console.WriteLine($"Properties: {config.Statistics.PropertyCount:N0}");
Console.WriteLine($"Booleans: {config.Statistics.BooleanCount:N0}");
Console.WriteLine($"Integers: {config.Statistics.IntegerCount:N0}");
Console.WriteLine($"Strings: {config.Statistics.StringCount:N0}");
Console.WriteLine($"Markers: {config.Statistics.MarkerCount:N0}");
Console.WriteLine($"Rules sections: {config.Statistics.RulesCount:N0}");
// Count property names
var propertyNames = config.RootNodes
.Where(n => n.NodeType == "property")
.GroupBy(n => n.Name)
.OrderByDescending(g => g.Count())
.Take(20);
if (propertyNames.Any())
{
Console.WriteLine($"\n=== Top 20 Property Names ===");
foreach (var group in propertyNames)
{
Console.WriteLine($" {group.Key}: {group.Count()} occurrences");
}
}
}
}
}

View File

@@ -0,0 +1,464 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GeViSetEditor.Core.Models;
namespace GeViSetEditor.Core.Parsers
{
/// <summary>
/// Enhanced parser that extracts full configuration structure
/// while preserving original binary data for round-trip conversion
/// </summary>
public class EnhancedSetFileParser
{
private byte[] _originalData;
private int _position;
public EnhancedSetFile Parse(byte[] data)
{
_originalData = data;
_position = 0;
var setFile = new EnhancedSetFile
{
OriginalData = data,
FileSize = data.Length
};
Console.WriteLine($"\n=== Enhanced Configuration Parser ===");
Console.WriteLine($"File size: {data.Length:N0} bytes\n");
// Read header
if (_position < data.Length && data[_position] == 0x00)
_position++;
string header = ReadPascalString();
setFile.Header = header ?? "GeViSoft Parameters";
Console.WriteLine($"Header: '{setFile.Header}'");
// Extract configuration items (key-value pairs)
ExtractConfigItems(setFile);
// Extract action mappings
ExtractActionMappings(setFile);
// Extract section names
ExtractSectionNames(setFile);
Console.WriteLine($"\n=== Parse Summary ===");
Console.WriteLine($"Configuration items: {setFile.ConfigItems.Count}");
Console.WriteLine($"Action mappings: {setFile.ActionMappings.Count}");
Console.WriteLine($"Section names: {setFile.SectionNames.Count}");
return setFile;
}
private void ExtractConfigItems(EnhancedSetFile setFile)
{
Console.WriteLine("\nExtracting configuration items...");
// Look for key-value patterns
// Common pattern: Pascal string (key) followed by typed value
for (int i = 0; i < _originalData.Length - 10; i++)
{
var item = TryReadConfigItem(i);
if (item != null && !string.IsNullOrWhiteSpace(item.Key))
{
setFile.ConfigItems.Add(item);
}
}
Console.WriteLine($"Found {setFile.ConfigItems.Count} config items");
}
private ConfigItemEntry TryReadConfigItem(int offset)
{
try
{
// Skip if this looks like part of an action mapping
if (IsNearRulesMarker(offset, 100))
return null;
// Skip if in the first 1000 bytes (header area)
if (offset < 1000)
return null;
// Try to read Pascal string as key
int pos = offset;
// Look for string marker (0x07) followed by reasonable length
if (pos >= _originalData.Length || _originalData[pos] != 0x07)
return null;
pos++;
if (pos >= _originalData.Length)
return null;
byte keyLen = _originalData[pos];
if (keyLen < 3 || keyLen > 50) // More restrictive length
return null;
pos++;
if (pos + keyLen > _originalData.Length)
return null;
string key = Encoding.UTF8.GetString(_originalData, pos, keyLen);
// Check if key looks valid (must be clean identifier)
if (!IsValidConfigKey(key))
return null;
// Key must not contain common section names (avoid duplicates)
string keyLower = key.ToLower();
if (keyLower.Contains("rules") || keyLower.Contains("action") ||
keyLower.Contains("gsc") || keyLower.Contains("gng"))
return null;
pos += keyLen;
// Must be followed immediately by typed value marker
if (pos >= _originalData.Length)
return null;
byte typeMarker = _originalData[pos];
if (typeMarker != 0x01 && typeMarker != 0x04 && typeMarker != 0x07)
return null;
// Try to read value
var (value, valueType, bytesRead) = TryReadTypedValue(pos);
if (value == null || bytesRead == 0)
return null;
return new ConfigItemEntry
{
FileOffset = offset,
Key = key,
Value = value,
ValueType = valueType
};
}
catch
{
return null;
}
}
private (object value, string type, int bytesRead) TryReadTypedValue(int pos)
{
if (pos >= _originalData.Length)
return (null, null, 0);
byte typeMarker = _originalData[pos];
try
{
switch (typeMarker)
{
case 0x01: // Boolean
if (pos + 2 <= _originalData.Length)
{
bool value = _originalData[pos + 1] != 0;
return (value, "boolean", 2);
}
break;
case 0x04: // Integer (4 bytes)
if (pos + 5 <= _originalData.Length)
{
int value = BitConverter.ToInt32(_originalData, pos + 1);
return (value, "integer", 5);
}
break;
case 0x07: // String (Pascal string)
pos++;
if (pos >= _originalData.Length)
break;
byte strLen = _originalData[pos];
if (strLen > 0 && strLen < 200 && pos + 1 + strLen <= _originalData.Length)
{
string value = Encoding.UTF8.GetString(_originalData, pos + 1, strLen);
if (IsValidStringValue(value))
{
return (value, "string", 2 + strLen);
}
}
break;
}
}
catch
{
// Ignore parse errors
}
return (null, null, 0);
}
private bool IsValidConfigKey(string key)
{
if (string.IsNullOrWhiteSpace(key))
return false;
// Must not be too long
if (key.Length > 40)
return false;
// No control characters except spaces
if (key.Any(c => char.IsControl(c)))
return false;
// Should contain mostly alphanumeric or underscores
int validChars = key.Count(c => char.IsLetterOrDigit(c) || c == '_' || c == '-');
// At least 90% valid chars (more strict)
if (validChars < key.Length * 0.9)
return false;
// Must start with a letter or underscore
if (!char.IsLetter(key[0]) && key[0] != '_')
return false;
// Common patterns that indicate it's not a config key
if (key.Contains(" ") || key.Contains("\t"))
return false;
return true;
}
private bool IsValidStringValue(string value)
{
if (string.IsNullOrEmpty(value))
return true; // Empty strings are valid
// Check for excessive control characters
int controlChars = value.Count(c => char.IsControl(c) && c != '\r' && c != '\n' && c != '\t');
return controlChars < value.Length * 0.3; // Less than 30% control chars
}
private bool IsNearRulesMarker(int offset, int distance)
{
// Check if there's a "Rules" marker nearby
byte[] rulesPattern = new byte[] { 0x05, 0x52, 0x75, 0x6C, 0x65, 0x73 };
int start = Math.Max(0, offset - distance);
int end = Math.Min(_originalData.Length - rulesPattern.Length, offset + distance);
for (int i = start; i < end; i++)
{
if (MatchesPattern(_originalData, i, rulesPattern))
return true;
}
return false;
}
private void ExtractActionMappings(EnhancedSetFile setFile)
{
Console.WriteLine("\nExtracting action mappings...");
// Search for all "Rules" patterns (05 52 75 6C 65 73)
byte[] rulesPattern = new byte[] { 0x05, 0x52, 0x75, 0x6C, 0x65, 0x73 };
for (int i = 0; i < _originalData.Length - rulesPattern.Length - 100; i++)
{
if (MatchesPattern(_originalData, i, rulesPattern))
{
var mapping = TryExtractActionMapping(i);
if (mapping != null)
{
setFile.ActionMappings.Add(mapping);
}
}
}
Console.WriteLine($"Found {setFile.ActionMappings.Count} action mappings");
}
private ActionMappingEntry TryExtractActionMapping(int rulesOffset)
{
try
{
var entry = new ActionMappingEntry
{
FileOffset = rulesOffset,
RulesMarkerOffset = rulesOffset
};
// Position after "Rules" marker
int pos = rulesOffset + 6; // Skip "05 Rules"
if (pos + 10 > _originalData.Length) return null;
// Skip metadata bytes (usually 0x00-0x04)
while (pos < _originalData.Length && _originalData[pos] <= 0x04)
{
pos++;
}
int actionDataStart = pos;
// Extract action strings using pattern 07 01 40 <len_2bytes> <data>
var actions = new List<string>();
int consecutiveFailures = 0;
int maxAttempts = 200;
while (maxAttempts-- > 0 && pos < _originalData.Length - 10)
{
var action = TryReadActionStringAt(pos);
if (action != null)
{
actions.Add(action);
pos += 5 + Encoding.UTF8.GetByteCount(action);
consecutiveFailures = 0;
}
else
{
pos++;
consecutiveFailures++;
if (consecutiveFailures > 100) break;
}
}
if (actions.Count > 0)
{
entry.Actions = actions;
entry.ActionDataStartOffset = actionDataStart;
entry.ActionDataEndOffset = pos;
// Store original bytes
int length = pos - rulesOffset;
entry.OriginalBytes = new byte[length];
Array.Copy(_originalData, rulesOffset, entry.OriginalBytes, 0, length);
return entry;
}
}
catch (Exception ex)
{
Console.WriteLine($"Error at offset {rulesOffset}: {ex.Message}");
}
return null;
}
private string TryReadActionStringAt(int pos)
{
// Pattern: 07 01 40 <len_2bytes_LE> <data>
if (pos + 5 > _originalData.Length) return null;
if (_originalData[pos] != 0x07 ||
_originalData[pos + 1] != 0x01 ||
_originalData[pos + 2] != 0x40)
return null;
ushort length = BitConverter.ToUInt16(_originalData, pos + 3);
if (length == 0 || length > 500 || pos + 5 + length > _originalData.Length)
return null;
try
{
string result = Encoding.UTF8.GetString(_originalData, pos + 5, length);
// Validate it looks like an action string
if (result.Length > 3 && result.All(c => c >= 32 || c == '\t' || c == '\n' || c == '\r'))
{
return result;
}
}
catch
{
// Ignore decode errors
}
return null;
}
private void ExtractSectionNames(EnhancedSetFile setFile)
{
Console.WriteLine("\nExtracting section names...");
// Look for Pascal strings that appear to be section names
// Characteristics: reasonable length (5-50 chars), mostly alphanumeric
HashSet<string> foundNames = new HashSet<string>();
for (int i = 0; i < _originalData.Length - 10; i++)
{
if (_originalData[i] == 0x07) // String marker
{
i++;
if (i >= _originalData.Length)
break;
byte len = _originalData[i];
if (len >= 5 && len <= 50 && i + 1 + len <= _originalData.Length)
{
try
{
string name = Encoding.UTF8.GetString(_originalData, i + 1, len);
// Check if it looks like a section name
if (IsLikelySectionName(name) && !foundNames.Contains(name))
{
foundNames.Add(name);
setFile.SectionNames.Add(new SectionNameEntry
{
FileOffset = i - 1,
Name = name
});
}
}
catch
{
// Ignore decode errors
}
}
}
}
Console.WriteLine($"Found {setFile.SectionNames.Count} unique section names");
}
private bool IsLikelySectionName(string name)
{
if (string.IsNullOrWhiteSpace(name))
return false;
// Should be mostly letters, numbers, underscores
// Common section names: "Alarms", "Cameras", "GeViIO", "Description", "Name", "IpHost"
int validChars = name.Count(c => char.IsLetterOrDigit(c) || c == '_' || c == ' ');
// At least 80% valid characters
return validChars >= name.Length * 0.8;
}
private string ReadPascalString()
{
if (_position >= _originalData.Length)
return null;
byte length = _originalData[_position++];
if (length == 0 || _position + length > _originalData.Length)
return null;
string result = Encoding.UTF8.GetString(_originalData, _position, length);
_position += length;
return result;
}
private bool MatchesPattern(byte[] data, int offset, byte[] pattern)
{
if (offset + pattern.Length > data.Length)
return false;
for (int i = 0; i < pattern.Length; i++)
{
if (data[offset + i] != pattern[i])
return false;
}
return true;
}
}
}

View File

@@ -0,0 +1,150 @@
using System;
using System.Text;
using GeViSetEditor.Core.Models;
namespace GeViSetEditor.Core.Parsers
{
/// <summary>
/// Modifies configuration in-place to preserve binary structure
/// Safer approach for edit operations
/// </summary>
public class InPlaceConfigModifier
{
/// <summary>
/// Modify a node's value in the original data array
/// Only works for fixed-size types (boolean, integer)
/// </summary>
public bool ModifyNode(byte[] data, ConfigNode node, object newValue)
{
if (node == null || data == null)
return false;
switch (node.NodeType)
{
case "boolean":
return ModifyBoolean(data, node, newValue);
case "integer":
return ModifyInteger(data, node, newValue);
case "string":
// Strings are variable length - can only modify if same length
return ModifyString(data, node, newValue);
default:
return false;
}
}
private bool ModifyBoolean(byte[] data, ConfigNode node, object newValue)
{
try
{
// Boolean structure: 01 <value>
// Value is at StartOffset + 1
int valueOffset = node.StartOffset + 1;
if (valueOffset >= data.Length)
return false;
bool boolValue = Convert.ToBoolean(newValue);
data[valueOffset] = boolValue ? (byte)1 : (byte)0;
// Update node value
node.Value = boolValue;
return true;
}
catch
{
return false;
}
}
private bool ModifyInteger(byte[] data, ConfigNode node, object newValue)
{
try
{
// Integer structure: 04 <int32_LE>
// Value is at StartOffset + 1
int valueOffset = node.StartOffset + 1;
if (valueOffset + 4 > data.Length)
return false;
int intValue = Convert.ToInt32(newValue);
byte[] bytes = BitConverter.GetBytes(intValue);
Array.Copy(bytes, 0, data, valueOffset, 4);
// Update node value
node.Value = intValue;
return true;
}
catch
{
return false;
}
}
private bool ModifyString(byte[] data, ConfigNode node, object newValue)
{
try
{
// String structure: 07 <len> <data>
// Original length at StartOffset + 1
int lengthOffset = node.StartOffset + 1;
if (lengthOffset >= data.Length)
return false;
byte originalLength = data[lengthOffset];
string newString = newValue?.ToString() ?? "";
byte[] newBytes = Encoding.UTF8.GetBytes(newString);
// Can only modify if new string has same length
if (newBytes.Length != originalLength)
{
Console.WriteLine($" ⚠ Cannot modify string: length mismatch (original={originalLength}, new={newBytes.Length})");
return false;
}
// Copy new string data
int dataOffset = lengthOffset + 1;
Array.Copy(newBytes, 0, data, dataOffset, newBytes.Length);
// Update node value
node.Value = newString;
return true;
}
catch
{
return false;
}
}
/// <summary>
/// Check if a node can be modified in-place
/// </summary>
public bool CanModify(ConfigNode node, object newValue)
{
if (node == null)
return false;
switch (node.NodeType)
{
case "boolean":
case "integer":
return true;
case "string":
// String can only be modified if same length
string currentStr = node.Value?.ToString() ?? "";
string newStr = newValue?.ToString() ?? "";
return Encoding.UTF8.GetByteCount(currentStr) == Encoding.UTF8.GetByteCount(newStr);
default:
return false;
}
}
}
}

View File

@@ -0,0 +1,189 @@
using System;
using System.Collections.Generic;
using System.Text;
using GeViSetEditor.Core.Models;
namespace GeViSetEditor.Core.Parsers
{
/// <summary>
/// Safe parser that preserves original binary data for round-trip conversion
/// Only parses action mappings, keeps everything else as-is
/// </summary>
public class SafeSetFileParser
{
private byte[] _originalData;
private int _position;
public SafeSetFile Parse(byte[] data)
{
_originalData = data;
_position = 0;
var setFile = new SafeSetFile
{
OriginalData = data,
FileSize = data.Length
};
// Read header
if (_position < data.Length && data[_position] == 0x00)
_position++;
string header = ReadPascalString();
setFile.Header = header ?? "GeViSoft Parameters";
Console.WriteLine($"Parsing .set file: {data.Length} bytes, header: '{setFile.Header}'");
// Find and extract all action mappings without disturbing the rest
ExtractActionMappings(setFile);
return setFile;
}
private void ExtractActionMappings(SafeSetFile setFile)
{
// Search for all "Rules" patterns (05 52 75 6C 65 73)
byte[] rulesPattern = new byte[] { 0x05, 0x52, 0x75, 0x6C, 0x65, 0x73 };
for (int i = 0; i < _originalData.Length - rulesPattern.Length - 100; i++)
{
if (MatchesPattern(_originalData, i, rulesPattern))
{
var mapping = TryExtractActionMapping(i);
if (mapping != null)
{
setFile.ActionMappings.Add(mapping);
}
}
}
Console.WriteLine($"Extracted {setFile.ActionMappings.Count} action mappings");
}
private ActionMappingEntry TryExtractActionMapping(int rulesOffset)
{
try
{
var entry = new ActionMappingEntry
{
FileOffset = rulesOffset,
RulesMarkerOffset = rulesOffset
};
// Position after "Rules" marker
int pos = rulesOffset + 6; // Skip "05 Rules"
// Read what appears to be count/metadata
if (pos + 10 > _originalData.Length) return null;
// Skip metadata bytes
while (pos < _originalData.Length && _originalData[pos] <= 0x04)
{
pos++;
}
// Store start of action data
int actionDataStart = pos;
// Extract action strings using pattern 07 01 40 <len_2bytes> <data>
var actions = new List<string>();
int consecutiveFailures = 0;
int maxAttempts = 200;
while (maxAttempts-- > 0 && pos < _originalData.Length - 10)
{
var action = TryReadActionStringAt(pos);
if (action != null)
{
actions.Add(action);
pos += 5 + Encoding.UTF8.GetByteCount(action); // 07 01 40 + 2-byte len + data
consecutiveFailures = 0;
}
else
{
pos++;
consecutiveFailures++;
if (consecutiveFailures > 100) break;
}
}
if (actions.Count > 0)
{
entry.Actions = actions;
entry.ActionDataStartOffset = actionDataStart;
entry.ActionDataEndOffset = pos;
// Store original bytes for this mapping
int length = pos - rulesOffset;
entry.OriginalBytes = new byte[length];
Array.Copy(_originalData, rulesOffset, entry.OriginalBytes, 0, length);
return entry;
}
}
catch (Exception ex)
{
Console.WriteLine($"Error extracting mapping at offset {rulesOffset}: {ex.Message}");
}
return null;
}
private string TryReadActionStringAt(int pos)
{
// Pattern: 07 01 40 <len_2bytes> <data>
if (pos + 5 > _originalData.Length) return null;
if (_originalData[pos] != 0x07 ||
_originalData[pos + 1] != 0x01 ||
_originalData[pos + 2] != 0x40)
return null;
int length = BitConverter.ToUInt16(_originalData, pos + 3);
if (length == 0 || length > 500 || pos + 5 + length > _originalData.Length)
return null;
try
{
string result = Encoding.UTF8.GetString(_originalData, pos + 5, length);
// Validate it looks like an action string
if (string.IsNullOrWhiteSpace(result) || result.Length < 3)
return null;
return result;
}
catch
{
return null;
}
}
private bool MatchesPattern(byte[] data, int offset, byte[] pattern)
{
if (offset + pattern.Length > data.Length)
return false;
for (int i = 0; i < pattern.Length; i++)
{
if (data[offset + i] != pattern[i])
return false;
}
return true;
}
private string ReadPascalString()
{
if (_position + 2 > _originalData.Length || _originalData[_position] != 0x07)
return null;
byte length = _originalData[_position + 1];
if (_position + 2 + length > _originalData.Length || length == 0)
return null;
string result = Encoding.UTF8.GetString(_originalData, _position + 2, length);
_position += 2 + length;
return result;
}
}
}

View File

@@ -0,0 +1,559 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Linq;
using GeViSetEditor.Core.Models;
namespace GeViSetEditor.Core.Parsers
{
/// <summary>
/// Parser for GeViSet .set configuration files (binary format)
/// Format: Proprietary binary with sections, rules, and action mappings
/// </summary>
public class SetFileParser
{
private byte[] _data;
private int _position;
public GeViSetConfiguration Parse(string filePath)
{
_data = File.ReadAllBytes(filePath);
_position = 0;
var config = new GeViSetConfiguration();
// Read header
if (_data[_position] == 0x00) _position++; // Skip initial marker
config.Header = ReadPascalString() ?? "GeViSoft Parameters";
Console.WriteLine($"Parsing {filePath} ({_data.Length:N0} bytes)");
Console.WriteLine($"Header: {config.Header}\n");
// Parse all sections until end of file
int sectionCount = 0;
while (_position < _data.Length - 20)
{
var section = TryParseSection();
if (section != null)
{
config.Sections.Add(section);
sectionCount++;
Console.WriteLine($"Section {sectionCount}: {section.Name} ({section.Items.Count} items, {section.Rules.Count} rules) - pos: {_position}");
}
else
{
_position++; // Skip unknown byte
}
}
Console.WriteLine($"\nTotal: {sectionCount} sections parsed");
// Second pass: Find all "Rules" markers and extract action mappings directly
Console.WriteLine($"\nSearching for Rules sections...");
ExtractRulesDirectly(config);
return config;
}
private void ExtractRulesDirectly(GeViSetConfiguration config)
{
// Search for pattern: 05 52 75 6C 65 73 = "05 Rules"
byte[] rulesPattern = new byte[] { 0x05, 0x52, 0x75, 0x6C, 0x65, 0x73 };
for (int i = 0; i < _data.Length - rulesPattern.Length; i++)
{
bool match = true;
for (int j = 0; j < rulesPattern.Length; j++)
{
if (_data[i + j] != rulesPattern[j])
{
match = false;
break;
}
}
if (match)
{
Console.WriteLine($" Found Rules pattern at offset {i} (0x{i:X})");
_position = i + rulesPattern.Length;
// Create or find a section for these rules
var rulesSection = config.Sections.FirstOrDefault(s => s.Name == "ActionMappings");
if (rulesSection == null)
{
rulesSection = new Section { Name = "ActionMappings" };
config.Sections.Add(rulesSection);
}
// Try to parse rules from this location
ParseRulesSection(rulesSection);
Console.WriteLine($" Extracted {rulesSection.Rules.Count} rules");
}
}
}
private Section TryParseSection()
{
int startPos = _position;
// Try to read section name
string sectionName = ReadPascalString();
if (string.IsNullOrEmpty(sectionName) || sectionName.Length > 50)
{
_position = startPos;
return null;
}
var section = new Section { Name = sectionName };
// Parse section contents
int maxIterations = 1000; // Safety limit
while (_position < _data.Length - 10 && maxIterations-- > 0)
{
// Check for "Rules" subsection (can be Pascal string OR length-prefixed)
int savePos = _position;
// Try Pascal string first (0x07 marker)
string possibleRules = ReadPascalString();
if (possibleRules == "Rules")
{
Console.WriteLine($" Found Rules subsection (Pascal) at offset {savePos} in section '{sectionName}'");
ParseRulesSection(section);
break; // Rules typically end a section
}
// Rewind and try length-prefixed format (no 0x07 marker)
_position = savePos;
possibleRules = ReadLengthPrefixedString();
if (possibleRules == "Rules")
{
Console.WriteLine($" Found Rules subsection (LenPrefix) at offset {savePos} in section '{sectionName}'");
ParseRulesSection(section);
break; // Rules typically end a section
}
// Neither worked, rewind
_position = savePos;
// Try to read a config item
var item = TryReadConfigItem();
if (item != null)
{
section.Items.Add(item);
}
else
{
// Check if we've hit the next section or end
if (IsLikelySectionStart())
break;
_position++;
}
}
return section;
}
private void ParseRulesSection(Section section)
{
int startRuleCount = section.Rules.Count;
int startPos = _position;
// Skip count/metadata bytes
while (_position < _data.Length && _data[_position] <= 0x04)
{
_position++;
}
Console.WriteLine($" ParseRulesSection: start={startPos}, after skip={_position}, bytes: {BitConverter.ToString(_data, _position, Math.Min(20, _data.Length - _position))}");
// Parse rules - keep trying until we stop finding action strings
int maxRules = 100;
int attempts = 0;
int consecutiveFails = 0;
while (maxRules-- > 0 && _position < _data.Length - 20)
{
int beforePos = _position;
var rule = TryParseRule();
attempts++;
if (rule != null)
{
section.Rules.Add(rule);
Console.WriteLine($" Found rule at pos {beforePos}, now at {_position}");
consecutiveFails = 0;
}
else
{
_position++;
consecutiveFails++;
// Stop if we've had too many consecutive failures
if (consecutiveFails > 100)
{
Console.WriteLine($" Too many consecutive failures at {_position}, stopping");
break;
}
}
if (attempts > 200) break; // Safety limit
}
Console.WriteLine($" ParseRulesSection complete: extracted {section.Rules.Count - startRuleCount} rules");
}
private ActionRule TryParseRule()
{
int startPos = _position;
// Look for rule marker: 01 <id>
if (_position + 2 >= _data.Length || _data[_position] != 0x01)
{
_position = startPos;
return null;
}
var rule = new ActionRule
{
RuleId = _data[_position + 1]
};
_position += 2;
// Skip size bytes (05 00 00 00 or 0a 00 00 00 patterns)
if (_position + 4 < _data.Length && (_data[_position] == 0x05 || _data[_position] == 0x0a))
{
_position += 4;
}
// Read trigger properties (fields starting with .)
for (int i = 0; i < 30; i++) // Max 30 properties
{
if (_position >= _data.Length) break;
// Check for property field (01 <len> .<name>)
if (_data[_position] == 0x01 && _position + 2 < _data.Length)
{
int len = _data[_position + 1];
if (len > 0 && len < 50 && _position + 2 + len < _data.Length)
{
string propName = Encoding.UTF8.GetString(_data, _position + 2, len);
_position += 2 + len;
// Read boolean value
if (_position < _data.Length)
{
bool value = _data[_position++] != 0;
if (propName.StartsWith("."))
{
rule.TriggerProperties[propName] = value;
}
}
}
else
{
break;
}
}
else
{
break;
}
}
// Read main action string (07 01 40 <len_2bytes> <data>)
string mainAction = ReadActionString();
if (!string.IsNullOrEmpty(mainAction))
{
rule.MainAction = mainAction;
}
// Skip metadata (04 02 40... patterns)
SkipMetadata();
// Look for nested Rules section (action variations)
int checkPos = _position;
string nestedRules = ReadPascalString();
if (nestedRules == "Rules")
{
ParseActionVariations(rule);
}
else
{
// Try length-prefixed format
_position = checkPos;
nestedRules = ReadLengthPrefixedString();
if (nestedRules == "Rules")
{
ParseActionVariations(rule);
}
else
{
_position = checkPos; // Rewind if not "Rules"
}
}
// Only return rule if it has meaningful data
if (!string.IsNullOrEmpty(rule.MainAction) || rule.TriggerProperties.Count > 0)
{
return rule;
}
_position = startPos;
return null;
}
private void ParseActionVariations(ActionRule rule)
{
// Skip count bytes
while (_position < _data.Length && _data[_position] <= 0x04)
{
_position++;
}
// Parse each variation
int maxVariations = 30;
while (maxVariations-- > 0 && _position < _data.Length - 20)
{
var variation = TryParseActionVariation();
if (variation != null)
{
rule.ActionVariations.Add(variation);
}
else
{
if (IsLikelySectionStart())
break;
// Look ahead for next variation
bool foundNext = false;
for (int i = 1; i < 20 && _position + i < _data.Length; i++)
{
if (_data[_position + i] == 0x01 && _position + i + 1 < _data.Length)
{
_position += i;
foundNext = true;
break;
}
}
if (!foundNext)
break;
}
}
}
private ActionVariation TryParseActionVariation()
{
int startPos = _position;
// Look for variation marker: 01 <id>
if (_position + 2 >= _data.Length || _data[_position] != 0x01)
{
_position = startPos;
return null;
}
var variation = new ActionVariation
{
VariationId = _data[_position + 1]
};
_position += 2;
// Skip size (05 00 00 00)
if (_position + 4 < _data.Length && _data[_position] == 0x05)
{
_position += 4;
}
// Read action string
variation.ActionString = ReadActionString() ?? "";
// Skip metadata
SkipMetadata();
// Read action type (GscAction, GCoreAction, etc.)
string actionType = ReadPascalString();
if (actionType != null && actionType.Contains("Action"))
{
variation.ActionType = actionType;
// Read full command (another action string)
variation.FullCommand = ReadActionString() ?? "";
// Read server type
variation.ServerType = ReadPascalString() ?? "";
// Try to read server name
int savePos = _position;
string serverName = ReadPascalString();
if (!string.IsNullOrEmpty(serverName) && serverName.Length < 100 && !serverName.StartsWith("."))
{
variation.ServerName = serverName;
}
else
{
_position = savePos; // Rewind if not a valid server name
}
}
// Only return if we got meaningful data
if (!string.IsNullOrEmpty(variation.ActionString))
{
return variation;
}
_position = startPos;
return null;
}
private ConfigItem TryReadConfigItem()
{
int startPos = _position;
string name = ReadPascalString();
if (string.IsNullOrEmpty(name) || name.Length > 100)
{
_position = startPos;
return null;
}
// Try to read value
object value = ReadValue();
if (value == null)
{
_position = startPos;
return null;
}
return new ConfigItem
{
Name = name,
Value = value,
Type = value switch
{
bool => ConfigValueType.Boolean,
int => ConfigValueType.Integer,
string => ConfigValueType.String,
_ => ConfigValueType.Binary
}
};
}
// Helper methods for reading binary data
private string ReadPascalString()
{
if (_position + 2 > _data.Length || _data[_position] != 0x07)
return null;
byte length = _data[_position + 1];
if (_position + 2 + length > _data.Length || length == 0)
return null;
string result = Encoding.UTF8.GetString(_data, _position + 2, length);
_position += 2 + length;
return result;
}
private string ReadLengthPrefixedString()
{
// Simpler format: just <length_byte> <string_data> (no 0x07 marker)
if (_position + 1 > _data.Length)
return null;
byte length = _data[_position];
if (length == 0 || length > 50 || _position + 1 + length > _data.Length)
return null;
string result = Encoding.UTF8.GetString(_data, _position + 1, length);
_position += 1 + length;
return result;
}
private string ReadActionString()
{
// Action strings: 07 01 40 <len_2bytes> <data>
if (_position + 5 > _data.Length ||
_data[_position] != 0x07 ||
_data[_position + 1] != 0x01 ||
_data[_position + 2] != 0x40)
return null;
int length = BitConverter.ToUInt16(_data, _position + 3);
if (_position + 5 + length > _data.Length || length > 500)
return null;
string result = Encoding.UTF8.GetString(_data, _position + 5, length);
_position += 5 + length;
return result;
}
private object ReadValue()
{
if (_position >= _data.Length)
return null;
byte type = _data[_position];
switch (type)
{
case 0x01: // Boolean
if (_position + 2 <= _data.Length)
{
_position++;
return _data[_position++] != 0;
}
break;
case 0x04: // Integer
if (_position + 5 <= _data.Length)
{
_position++;
int value = BitConverter.ToInt32(_data, _position);
_position += 4;
return value;
}
break;
case 0x07: // String
return ReadPascalString();
}
return null;
}
private void SkipMetadata()
{
// Skip common metadata patterns (04 02 40... and 04 0a...)
while (_position + 5 < _data.Length && _data[_position] == 0x04)
{
_position += 5;
}
}
private string PeekString(int length)
{
if (_position + length > _data.Length)
return "";
return Encoding.UTF8.GetString(_data, _position, length);
}
private bool IsLikelySectionStart()
{
if (_position + 10 >= _data.Length)
return true;
// Check for multiple nulls (section boundary)
int nullCount = 0;
for (int i = 0; i < Math.Min(10, _data.Length - _position); i++)
{
if (_data[_position + i] == 0x00)
nullCount++;
}
return nullCount >= 4;
}
}
}