Skip to content

Commit

Permalink
Merge pull request #10 from misprit7/terracc-improvements
Browse files Browse the repository at this point in the history
Terracc improvements
  • Loading branch information
misprit7 authored Jun 12, 2024
2 parents 942b2ee + 8ba66ac commit c42120f
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 27 deletions.
9 changes: 3 additions & 6 deletions Accelerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,8 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Terraria;
using Terraria.DataStructures;
using Terraria.Enums;
using Terraria.ID;
using Terraria.ModLoader;

Expand Down Expand Up @@ -680,7 +675,9 @@ public static void Preprocess()
pbCoord2Id = pbId2Coord.Select((s, i) => new { s, i }).ToDictionary(x => x.s, x => x.i);
numPb = pbId2Coord.Count();
// Hardcoded default
clockGroup = wireGroup[default_clock_coord.X, default_clock_coord.Y, default_clock_clr];
if(default_clock_coord.X < Main.maxTilesX && default_clock_coord.Y < Main.maxTilesY){
clockGroup = wireGroup[default_clock_coord.X, default_clock_coord.Y, default_clock_clr];
}
}

/*
Expand Down
1 change: 0 additions & 1 deletion Commands/AccelCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ public static void Exec(string[] args)
TerraCC.compile();
}
TerraCC.enable();
Console.WriteLine("Executed compile command");
});
break;
default:
Expand Down
88 changes: 75 additions & 13 deletions TerraCC.cs
Original file line number Diff line number Diff line change
Expand Up @@ -183,27 +183,25 @@ private static string faulty_str(){
int max_connections = extra_std.Max(list => list.Count());
std_lamps.Append($"#define max_connections {max_connections}\n");
std_lamps.Append(
$"static int std_lamps[{Accelerator.numGroups}][{max_connections}][3][{Accelerator.colors}] = {{\n"
$"static int std_lamps[{Accelerator.numGroups}][{max_connections}][3][{Accelerator.colors}];"
);
write_file("std_lamps.h", std_lamps.ToString());

StringBuilder std_lamps_data = new StringBuilder();
for(int g = 0; g < Accelerator.numGroups; ++g){
if(extra_std[g].Count() == 0){
std_lamps.Append("{0},\n");
continue;
}
std_lamps.Append("{");
std_lamps_data.Append("{\n");
foreach(var std_lamp in extra_std[g]){
std_lamps.Append("{");
std_lamps_data.Append("{");
for(int i = 0; i < 3; ++i){
std_lamps.Append($"{{{std_lamp[i,0]},{std_lamp[i,1]},{std_lamp[i,2]},{std_lamp[i,3]}}},");
std_lamps_data.Append($"{{{std_lamp[i,0]},{std_lamp[i,1]},{std_lamp[i,2]},{std_lamp[i,3]}}},");
}
std_lamps.Append("},");
std_lamps_data.Append("},\n");
}
std_lamps.Append("},\n");
std_lamps_data.Append("},\n");
}

std_lamps.Append("};\n");

write_file("std_lamps.c", std_lamps.ToString());
write_file("std_lamps_data.txt", std_lamps_data.ToString());

return $@"
for(int j = 0; j < max_connections; ++j){{
Expand Down Expand Up @@ -245,6 +243,7 @@ public static void transpile()
#include <stdint.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stddef.h>
Expand All @@ -267,7 +266,54 @@ public static void transpile()
static int clock_count = 0;

// Standard lamp connections
#include ""std_lamps.c""
#include ""std_lamps.h""

void init(){{
FILE *std_lamps_f = fopen(""/tmp/terracc/std_lamps_data.txt"", ""r"");
if(std_lamps_f == NULL){{
perror(""Failed to open std_lamps_data.txt!"");
return;
}}
for(int i = 0; i < sizeof(std_lamps)/sizeof(std_lamps[0]); ++i){{
char line[100];

// Gets first {{,
fgets(line, sizeof(line), std_lamps_f);
if(line[0] != '{{'){{
printf(""Read format not as expected!"");
}}
// Gets data
fgets(line, sizeof(line), std_lamps_f);

int j = 0;
while(sscanf(line, ""{{{{%d,%d,%d,%d}},{{%d,%d,%d,%d}},{{%d,%d,%d,%d}},}},"",
&std_lamps[i][j][0][0], &std_lamps[i][j][0][1], &std_lamps[i][j][0][2], &std_lamps[i][j][0][3],
&std_lamps[i][j][1][0], &std_lamps[i][j][1][1], &std_lamps[i][j][1][2], &std_lamps[i][j][1][3],
&std_lamps[i][j][2][0], &std_lamps[i][j][2][1], &std_lamps[i][j][2][2], &std_lamps[i][j][2][3]
) == 12){{
fgets(line, sizeof(line), std_lamps_f);
++j;
}}
// Data has been read so line should be }},
if(line[0] != '}}'){{
printf(""Read format not as expected!"");
}}
}}
fclose(std_lamps_f);

/* FILE *file; */
/* char *filename = ""/tmp/test.txt""; */

/* // Open the file for writing */
/* file = fopen(filename, ""w""); */
/* // Write data to the file */
/* fprintf(file, ""Finished init\n""); */

/* // Close the file */
/* fclose(file); */

/* /1* printf(""Finished init!\n""); *1/ */
}}

void trigger(int input_groups[][colors], int32_t num_inputs){{
/* printf(""input: %d\n"", input_groups[0][0]); */
Expand Down Expand Up @@ -336,6 +382,9 @@ void set_clock(int group){{
}}

int main(void){{

init();

int triggers[1][4] = {{{{4, -1, -1, -1}}}};
trigger(triggers, 1);

Expand All @@ -352,6 +401,13 @@ int main(void){{
printf(""%d "", pb_states[i]);
}}
printf(""\n"");

for(int i = 0; i < sizeof(std_lamps) / sizeof(std_lamps[0]); ++i){{
for(int j = 0; j < 12; ++j){{
printf(""%d, "", std_lamps[i][0][0][j]);
}}
printf(""\n"");
}}
}}

";
Expand Down Expand Up @@ -414,14 +470,18 @@ public static void enable(){
IntPtr read_pb_ptr = dlsym(libHandle, "read_pb");
IntPtr read_clock_ptr = dlsym(libHandle, "read_clock");
IntPtr set_clock_ptr = dlsym(libHandle, "set_clock");
IntPtr init_ptr = dlsym(libHandle, "init");

trigger = Marshal.GetDelegateForFunctionPointer<TriggerDelegate>(trigger_ptr);
read_states = Marshal.GetDelegateForFunctionPointer<ReadStatesDelegate>(read_states_ptr);
read_pb = Marshal.GetDelegateForFunctionPointer<ReadPbDelegate>(read_pb_ptr);
read_clock = Marshal.GetDelegateForFunctionPointer<ReadClockDelegate>(read_clock_ptr);
set_clock = Marshal.GetDelegateForFunctionPointer<SetClockDelegate>(set_clock_ptr);
init = Marshal.GetDelegateForFunctionPointer<InitDelegate>(init_ptr);

WireHead.useTerracc = true;
Console.WriteLine("Reading in lamp data");
init();
if(Accelerator.clockGroup != -1){
set_clock(Accelerator.clockGroup);
}
Expand All @@ -443,6 +503,7 @@ public static void disable(){
public static ReadPbDelegate read_pb;
public static ReadClockDelegate read_clock;
public static SetClockDelegate set_clock;
public static InitDelegate init;

/**********************************************************************
* Imported functions
Expand All @@ -465,5 +526,6 @@ public static void disable(){
public delegate void ReadPbDelegate([Out] byte[] pb_states);
public delegate int ReadClockDelegate();
public delegate void SetClockDelegate(int group);
public delegate void InitDelegate();

}
6 changes: 0 additions & 6 deletions WireHead.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
using Terraria.ModLoader;
using Terraria.IO;
using WireHead.Commands;
using System;
using System.Collections.Concurrent;
using System.Runtime.InteropServices;
using Steamworks;
using Terraria;
using SteelSeries.GameSense;
using Microsoft.Xna.Framework;
using Terraria.DataStructures;
using Terraria.GameContent.Tile_Entities;
/* using IL.Terraria.ID; */
using Terraria.ID;

namespace WireHead
Expand Down
2 changes: 1 addition & 1 deletion WireHead.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<Import Project="..\tModLoader.targets" />
<PropertyGroup>
<AssemblyName>WireHead</AssemblyName>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<PlatformTarget>x64</PlatformTarget>
<LangVersion>latest</LangVersion>
</PropertyGroup>
Expand Down

0 comments on commit c42120f

Please sign in to comment.