Skip to content

Commit

Permalink
working character customizer api call
Browse files Browse the repository at this point in the history
  • Loading branch information
diogotr7 committed Oct 12, 2024
1 parent 592c79f commit 17be7fb
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 87 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -207,3 +207,4 @@ src/StarBreaker.Grpc/protos/*
#un-exclude the ones we need that happen to be in the same folder :)
!src/StarBreaker.Grpc/protos/google/
!src/StarBreaker.Grpc/protos/protoc-gen-openapiv2/
src/StarBreaker.Debug/env.json
1 change: 1 addition & 0 deletions src/StarBreaker.Debug/Environment.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
internal record Environment(string Url, string Token);
86 changes: 86 additions & 0 deletions src/StarBreaker.Debug/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
using System.Text.Json;
using Google.Protobuf;
using Grpc.Core;
using Grpc.Net.Client;
using Sc.External.Services.CharacterCustomizer.V1;

var env = JsonSerializer.Deserialize<Environment>(File.ReadAllText("env.json"));
if (env == null || string.IsNullOrWhiteSpace(env.Token) || string.IsNullOrWhiteSpace(env.Url))
{
Console.WriteLine("Failed to read env.json");
return;
}

var creds = CallCredentials.FromInterceptor((_, metadata) =>
{
metadata.Add("Authorization", $"Bearer {env.Token}");
return Task.CompletedTask;
});

var channel = GrpcChannel.ForAddress(env.Url, new GrpcChannelOptions
{
Credentials = ChannelCredentials.Create(ChannelCredentials.SecureSsl, creds)
});
var charClient = new CharacterCustomizerService.CharacterCustomizerServiceClient(channel);

const string testChar = @"C:\Users\Diogo\Downloads\default_f_diff_eyecolor.grpc";

var bytes = GrpcToProtoBytes(File.ReadAllBytes(testChar));
var character = SaveCharacterCustomizationsRequest.Parser.ParseFrom(bytes);

ChangeDna(character);
ChangeEyeColor(character);
ChangeBodyColor(character);

var response = charClient.SaveCharacterCustomizations(character);

Console.WriteLine(response);
return;

//TODO: make this not stupid
byte[] GrpcToProtoBytes(byte[] proto)
{
return proto[5..];
}

void ChangeDna(SaveCharacterCustomizationsRequest req)
{
req.CharacterCustomizations.DnaMatrix = ByteString.CopyFrom(Convert.FromHexString(
"9493d0fc54ebf49e0452d765000000000c00040004000000feff1700feff1700feff1700feff1700feff1700feff1700feff1700feff1700feff1700feff1700feff1700feff1700000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"));
}

void ChangeEyeColor(SaveCharacterCustomizationsRequest saveCharacterCustomizationsRequest)
{
ChangeColorWithId(saveCharacterCustomizationsRequest, 0x442a34ac, 214, 30, 30);
}

void ChangeBodyColor(SaveCharacterCustomizationsRequest saveCharacterCustomizationsRequest)
{
//only changes head for now, todo
ChangeColorWithId(saveCharacterCustomizationsRequest, 0xbd530797, 30, 30, 200);
}

void ChangeColorWithId(SaveCharacterCustomizationsRequest req, uint id, byte r, byte g, byte b)
{
var materialParams = req.CharacterCustomizations.CustomMaterialParams;
if (materialParams == null)
return;

var copy = new byte[materialParams.Length];
materialParams.CopyTo(copy, 0);
var span = copy.AsSpan();
ReadOnlySpan<byte> colorKey = BitConverter.GetBytes(id);

var colorIndex = span.IndexOf(colorKey);
if (colorIndex == -1)
return;

var colorLocation = colorIndex + colorKey.Length;
span[colorLocation + 0] = r; //R
span[colorLocation + 1] = g; //G
span[colorLocation + 2] = b; //B
span[colorLocation + 3] = 0xFF; //A

req.CharacterCustomizations.CustomMaterialParams = ByteString.CopyFrom(copy);
}
16 changes: 16 additions & 0 deletions src/StarBreaker.Debug/StarBreaker.Debug.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\StarBreaker.Grpc\StarBreaker.Grpc.csproj"/>
<Content Include="env.json" CopyToOutputDirectory="PreserveNewest"/>
</ItemGroup>


</Project>
86 changes: 0 additions & 86 deletions src/StarBreaker.Grpc/Program.cs

This file was deleted.

2 changes: 1 addition & 1 deletion src/StarBreaker.Grpc/StarBreaker.Grpc.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<!--Note: Don't add code to this project, keep it just the grpc clients. Otherwise compilation takes ages-->

<PropertyGroup>
<OutputType>exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
Expand Down
6 changes: 6 additions & 0 deletions src/StarBreaker.sln
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StarBreaker.Grpc", "StarBre
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StarBreaker.Protobuf", "StarBreaker.Protobuf\StarBreaker.Protobuf.csproj", "{DA5C9EDC-DB21-47E1-B276-780DFBCF9085}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StarBreaker.Debug", "StarBreaker.Debug\StarBreaker.Debug.csproj", "{782C1FBA-7709-4E83-A6D3-FA17F08CB5FE}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -84,5 +86,9 @@ Global
{DA5C9EDC-DB21-47E1-B276-780DFBCF9085}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DA5C9EDC-DB21-47E1-B276-780DFBCF9085}.Release|Any CPU.ActiveCfg = Release|Any CPU
{DA5C9EDC-DB21-47E1-B276-780DFBCF9085}.Release|Any CPU.Build.0 = Release|Any CPU
{782C1FBA-7709-4E83-A6D3-FA17F08CB5FE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{782C1FBA-7709-4E83-A6D3-FA17F08CB5FE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{782C1FBA-7709-4E83-A6D3-FA17F08CB5FE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{782C1FBA-7709-4E83-A6D3-FA17F08CB5FE}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal

0 comments on commit 17be7fb

Please sign in to comment.