mirror of
https://gitlab.com/hashborgir/d2tools.git
synced 2025-10-13 16:34:23 -05:00
TBL editor working, saves to txt file, DB, TBL, works in game
This commit is contained in:
170
TblEditor.php
170
TblEditor.php
@@ -24,43 +24,72 @@ try {
|
||||
$cmd = $_REQUEST['cmd'];
|
||||
$tableName = $_REQUEST['tableName'];
|
||||
|
||||
function dumpAndProcessTable($tableName) {
|
||||
// Set the path to the SQLite3 executable
|
||||
$sqlitePath = "bin\sqlite3.exe";
|
||||
|
||||
// Dump the SQLite table to a TSV file
|
||||
$tsvFile = $tableName . ".txt";
|
||||
$dumpCommand = "bin\sqlite3.exe {$_SESSION['modname']}.db \"SELECT * FROM string\" -separator \"\t\" -cmd \".mode tabs\" -cmd \".headers off\" > \"{$_SESSION['tbl']}{$tsvFile}\"";
|
||||
|
||||
shell_exec($dumpCommand);
|
||||
|
||||
// Run the executable with the TSV file as input
|
||||
$executablePath = "bin\EnquettarM.exe";
|
||||
$processCommand = "{$executablePath} -i \"{$_SESSION['tbl']}{$tsvFile}\"";
|
||||
|
||||
$etxt = 'ModString.txt';
|
||||
$etbl = 'ModString.tbl';
|
||||
|
||||
shell_exec($processCommand);
|
||||
|
||||
// now file is generated in rootdir, etbl, which we need to move to session tbl
|
||||
rename($etbl, "{$_SESSION['tbl']}/$tableName.tbl");
|
||||
|
||||
// Remove the generated ModString.tbl
|
||||
//unlink($etbl);
|
||||
}
|
||||
//function dumpAndProcessTable($tableName) {
|
||||
// // Set the path to the SQLite3 executable
|
||||
// $sqlitePath = "bin\sqlite3.exe";
|
||||
//
|
||||
// // Dump the SQLite table to a TSV file
|
||||
// $tsvFile = $tableName . ".txt";
|
||||
// $dumpCommand = "bin\sqlite3.exe {$_SESSION['modname']}.db \"SELECT * FROM string\" -separator \"\t\" -cmd \".mode tabs\" -cmd \".headers off\" > \"{$_SESSION['tbl']}{$tsvFile}\"";
|
||||
// shell_exec($dumpCommand);
|
||||
//
|
||||
// die();
|
||||
//
|
||||
// // Convert TSV to TBL
|
||||
// // Run the executable with the TSV file as input
|
||||
// $executablePath = "bin\EnquettarM.exe";
|
||||
// $processCommand = "{$executablePath} -i \"{$_SESSION['tbl']}{$tsvFile}\"";
|
||||
//
|
||||
// // will be saved into ModString.tbl
|
||||
//
|
||||
// $etxt = 'ModString.txt';
|
||||
// $etbl = 'ModString.tbl';
|
||||
//
|
||||
// shell_exec($processCommand);
|
||||
//
|
||||
// // now file is generated in rootdir, etbl, which we need to move to session tbl
|
||||
// rename($etbl, "{$_SESSION['tbl']}/$tableName.tbl");
|
||||
//
|
||||
// // Remove the generated ModString.tbl
|
||||
// //unlink($etbl);
|
||||
//}
|
||||
|
||||
if ($cmd == "getTable") {
|
||||
$query = "SELECT ROWID,* FROM $tableName";
|
||||
$rows = PDO_Execute($query);
|
||||
$filePath = "{$_SESSION['tbl']}/$tableName.tbl";
|
||||
$executablePath = getcwd() . "\bin\EnquettarM.exe";
|
||||
$processCommand = "{$executablePath} -e \"$filePath\"";
|
||||
shell_exec($processCommand);
|
||||
|
||||
rename(getcwd() . "\ModString.txt", "{$_SESSION['tbl']}/$tableName.txt");
|
||||
|
||||
$filePath = "{$_SESSION['tbl']}/$tableName.txt";
|
||||
|
||||
$fileLines = file($filePath);
|
||||
|
||||
$rows = [];
|
||||
$rowid = 1; // Starting rowid value
|
||||
|
||||
foreach ($fileLines as $line) {
|
||||
$row = explode("\t", $line);
|
||||
$key = $row[0];
|
||||
$string = $row[1];
|
||||
|
||||
$rowData = [
|
||||
'rowid' => $rowid,
|
||||
'Key' => $key,
|
||||
'String' => $string
|
||||
];
|
||||
|
||||
$rows[] = $rowData;
|
||||
$rowid++;
|
||||
}
|
||||
|
||||
|
||||
// Start the table with Bootstrap classes
|
||||
$tableMarkup = '';
|
||||
|
||||
// Iterate over the rows
|
||||
foreach ($rows as $row) {
|
||||
$string = str_replace('\n', " ", $row['String']);
|
||||
|
||||
$tableMarkup .= "<tr class='row-{$row['rowid']}'>";
|
||||
|
||||
$tableMarkup .= "<td style='width: 64px;'>{$row['rowid']}</td>";
|
||||
@@ -69,7 +98,7 @@ if ($cmd == "getTable") {
|
||||
$tableMarkup .= "<td style='width: 20%;'><input type='text' value='{$row['Key']}' class='form-control'><span style='visibility: hidden;'>{$row['Key']}</span></td>";
|
||||
|
||||
// Display the String column as a textarea with Bootstrap form-control class
|
||||
$tableMarkup .= "<td><textarea class='form-control'>{$string}</textarea></td>";
|
||||
$tableMarkup .= "<td><textarea class='form-control'>{$row['String']}</textarea></td>";
|
||||
|
||||
// Add the Update and Delete links within a single td element
|
||||
$tableMarkup .= "<td style='width: 100px;'><div>";
|
||||
@@ -78,7 +107,7 @@ if ($cmd == "getTable") {
|
||||
$tableMarkup .= "<a href='#' class='update-link btn btn-primary' data-rowid='{$row['rowid']}'>Update</a>";
|
||||
|
||||
// Add the Delete link with Bootstrap btn and btn-danger classes
|
||||
$tableMarkup .= "<a style='float:right' href='#' class='delete-link btn btn-danger' data-rowid='{$row['rowid']}'>Delete</a>";
|
||||
$tableMarkup .= "<a style='' href='#' class='delete-link btn btn-danger' data-rowid='{$row['rowid']}'>Delete</a>";
|
||||
|
||||
$tableMarkup .= "</div></td>";
|
||||
|
||||
@@ -91,16 +120,71 @@ if ($cmd == "getTable") {
|
||||
|
||||
|
||||
if ($cmd == "update") {
|
||||
$query = "UPDATE $tableName SET Key = ?, String = ? WHERE ROWID = ?";
|
||||
PDO_Execute($query, [$_REQUEST['key'], $_REQUEST['string'], $_REQUEST['rowid']]);
|
||||
dumpAndProcessTable($tableName);
|
||||
echo $_REQUEST['key'] . " updated";
|
||||
$query = "UPDATE $tableName SET Key = ?, String = ? WHERE Key = ?";
|
||||
PDO_Execute($query, [$_REQUEST['key'], $_REQUEST['string'], $_REQUEST['key']]);
|
||||
|
||||
// Update the corresponding row in the text file
|
||||
$filePath = "{$_SESSION['tbl']}/$tableName.txt";
|
||||
$fileLines = file($filePath);
|
||||
|
||||
// Calculate the line number to update based on the rowid
|
||||
$lineNumber = $_REQUEST['rowid'];
|
||||
|
||||
// Update the line in the file with the new key and string values
|
||||
$fileLines[$lineNumber - 1] = "{$_REQUEST['key']}\t{$_REQUEST['string']}\n";
|
||||
|
||||
// Save the updated content back to the text file
|
||||
file_put_contents($filePath, implode("", array_filter($fileLines)));
|
||||
|
||||
// Convert TXT to TBL
|
||||
$executablePath = "bin\EnquettarM.exe";
|
||||
$processCommand = "{$executablePath} -i \"{$_SESSION['tbl']}/$tableName.txt\"";
|
||||
|
||||
shell_exec($processCommand);
|
||||
|
||||
rename(getcwd() . "\ModString.tbl", "{$_SESSION['tbl']}/$tableName.tbl");
|
||||
|
||||
// Remove empty lines from end of file
|
||||
// Read the file contents into an array of lines
|
||||
$lines = file($filePath, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
|
||||
|
||||
// Trim empty lines from the end of the array
|
||||
while (!empty($lines) && trim(end($lines)) === '') {
|
||||
array_pop($lines);
|
||||
}
|
||||
|
||||
// Write the updated content back to the file
|
||||
file_put_contents($filePath, implode(PHP_EOL, $lines));
|
||||
}
|
||||
|
||||
|
||||
if ($cmd == "delete") {
|
||||
$query = "DELETE FROM $tableName WHERE ROWID = ?";
|
||||
PDO_Execute($query, [$_REQUEST['rowid']]);
|
||||
dumpAndProcessTable($tableName);
|
||||
$query = "DELETE FROM $tableName WHERE Key = ?";
|
||||
PDO_Execute($query, [$_REQUEST['key']]);
|
||||
$rowid = $_REQUEST['rowid'];
|
||||
$filePath = "{$_SESSION['tbl']}/$tableName.txt";
|
||||
// Calculate the line number to delete based on the rowid
|
||||
$lineNumber = $rowid - 1; // Assuming zero-indexed line numbers
|
||||
// Read the lines of the text file into an array
|
||||
$fileLines = file($filePath, FILE_IGNORE_NEW_LINES);
|
||||
|
||||
// Remove the line at the specified line number
|
||||
if (isset($fileLines[$lineNumber])) {
|
||||
unset($fileLines[$lineNumber]);
|
||||
}
|
||||
|
||||
// Save the updated content back to the text file
|
||||
file_put_contents($filePath, implode("\n", $fileLines));
|
||||
|
||||
// Convert TXT to TBL
|
||||
$executablePath = "bin\EnquettarM.exe";
|
||||
$processCommand = "{$executablePath} -i \"{$_SESSION['tbl']}/$tableName.txt\"";
|
||||
|
||||
shell_exec($processCommand);
|
||||
|
||||
rename(getcwd() . "\ModString.tbl", "{$_SESSION['tbl']}/$tableName.tbl");
|
||||
|
||||
|
||||
echo $_REQUEST['rowid'] . " deleted";
|
||||
}
|
||||
|
||||
@@ -119,7 +203,15 @@ if ($cmd == "add") {
|
||||
// Retrieve the last inserted rowid from the table
|
||||
$query = "SELECT last_insert_rowid() AS rowid";
|
||||
$result = PDO_FetchOne($query);
|
||||
|
||||
// Append the new row to the text file
|
||||
$newLine = "{$key}\t{$string}";
|
||||
file_put_contents($filePath, $newLine . PHP_EOL, FILE_APPEND);
|
||||
|
||||
echo $result;
|
||||
dumpAndProcessTable($tableName);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user