modify compiler to get rid of errors in unity

This commit is contained in:
wsycarlos 2026-01-06 17:02:22 +08:00
parent bbf6aab1ac
commit e04721fab1
6 changed files with 43 additions and 19 deletions

View File

@ -57,6 +57,7 @@ t_netstd_generator::t_netstd_generator(t_program* program, const map<string, str
use_pascal_case_properties = false; use_pascal_case_properties = false;
union_ = false; union_ = false;
serialize_ = false; serialize_ = false;
unity_ = false;
wcf_ = false; wcf_ = false;
wcf_namespace_.clear(); wcf_namespace_.clear();
@ -72,6 +73,10 @@ t_netstd_generator::t_netstd_generator(t_program* program, const map<string, str
serialize_ = true; serialize_ = true;
wcf_namespace_ = iter->second; // since there can be only one namespace wcf_namespace_ = iter->second; // since there can be only one namespace
} }
else if (iter->first.compare("unity") == 0) {
unity_ = true;
target_net_version = 0;
}
else if (iter->first.compare("wcf") == 0) { else if (iter->first.compare("wcf") == 0) {
wcf_ = true; wcf_ = true;
wcf_namespace_ = iter->second; wcf_namespace_ = iter->second;
@ -126,6 +131,8 @@ bool t_netstd_generator::is_serialize_enabled() const { return serialize_; }
bool t_netstd_generator::is_union_enabled() const { return union_; } bool t_netstd_generator::is_union_enabled() const { return union_; }
bool t_netstd_generator::is_unity_enabled() const { return unity_; }
void t_netstd_generator::init_generator() void t_netstd_generator::init_generator()
{ {
MKDIR(get_out_dir().c_str()); MKDIR(get_out_dir().c_str());
@ -160,6 +167,7 @@ void t_netstd_generator::init_generator()
pverbose("- serialize ............ %s\n", (is_serialize_enabled() ? "ON" : "off")); pverbose("- serialize ............ %s\n", (is_serialize_enabled() ? "ON" : "off"));
pverbose("- wcf .................. %s\n", (is_wcf_enabled() ? "ON" : "off")); pverbose("- wcf .................. %s\n", (is_wcf_enabled() ? "ON" : "off"));
pverbose("- pascal ............... %s\n", (use_pascal_case_properties ? "ON" : "off")); pverbose("- pascal ............... %s\n", (use_pascal_case_properties ? "ON" : "off"));
pverbose("- unity ............... %s\n", (is_unity_enabled() ? "ON" : "off"));
pverbose("- target NET version ... %d\n", target_net_version); pverbose("- target NET version ... %d\n", target_net_version);
pverbose("- no_deepcopy .......... %s\n", (suppress_deepcopy ? "ON" : "off")); pverbose("- no_deepcopy .......... %s\n", (suppress_deepcopy ? "ON" : "off"));
pverbose("- async_postfix ........ %s\n", (add_async_postfix ? "ON" : "off")); pverbose("- async_postfix ........ %s\n", (add_async_postfix ? "ON" : "off"));
@ -202,20 +210,24 @@ void t_netstd_generator::reset_indent() {
void t_netstd_generator::pragmas_and_directives(ostream& out) void t_netstd_generator::pragmas_and_directives(ostream& out)
{ {
if( target_net_version >= 9) { if (!unity_)
out << "// targeting net 9" << '\n'; {
out << "#if( !NET9_0_OR_GREATER)" << '\n'; if (target_net_version >= 9) {
} else if( target_net_version >= 8) { out << "// targeting net 9" << '\n';
out << "// targeting net 8" << '\n'; out << "#if( !NET9_0_OR_GREATER)" << '\n';
out << "#if( NET9_0_OR_GREATER || !NET8_0_OR_GREATER)" << '\n'; }
} else { else if (target_net_version >= 8) {
out << "// targeting netstandard 2.x" << '\n'; out << "// targeting net 8" << '\n';
out << "#if(! NETSTANDARD2_0_OR_GREATER)" << '\n'; out << "#if( NET9_0_OR_GREATER || !NET8_0_OR_GREATER)" << '\n';
}
else {
out << "// targeting netstandard 2.x" << '\n';
out << "#if(! NETSTANDARD2_0_OR_GREATER)" << '\n';
}
out << "#error Unexpected target platform. See 'thrift --help' for details." << '\n';
out << "#endif" << '\n';
out << '\n';
} }
out << "#error Unexpected target platform. See 'thrift --help' for details." << '\n';
out << "#endif" << '\n';
out << '\n';
if( target_net_version >= 6) { if( target_net_version >= 6) {
out << "// Thrift code generated for net" << target_net_version << '\n'; out << "// Thrift code generated for net" << target_net_version << '\n';
out << "#nullable enable // requires C# 8.0" << '\n'; out << "#nullable enable // requires C# 8.0" << '\n';
@ -373,10 +385,14 @@ string t_netstd_generator::netstd_type_usings() const
"using System.Linq;\n" "using System.Linq;\n"
"using System.Threading;\n" "using System.Threading;\n"
"using System.Threading.Tasks;\n" "using System.Threading.Tasks;\n"
"using Microsoft.Extensions.Logging;\n"
"using Thrift;\n" "using Thrift;\n"
"using Thrift.Collections;\n"; "using Thrift.Collections;\n";
if (!is_unity_enabled())
{
namespaces += "using Microsoft.Extensions.Logging;\n";
}
if (is_wcf_enabled()) if (is_wcf_enabled())
{ {
namespaces += "using System.ServiceModel;\n"; namespaces += "using System.ServiceModel;\n";
@ -396,9 +412,13 @@ string t_netstd_generator::netstd_thrift_usings() const
"using Thrift.Protocol.Entities;\n" "using Thrift.Protocol.Entities;\n"
"using Thrift.Protocol.Utilities;\n" "using Thrift.Protocol.Utilities;\n"
"using Thrift.Transport;\n" "using Thrift.Transport;\n"
"using Thrift.Transport.Client;\n" "using Thrift.Transport.Client;\n";
"using Thrift.Transport.Server;\n"
"using Thrift.Processor;\n"; if (!is_unity_enabled())
{
namespaces += "using Thrift.Transport.Server;\n";
namespaces += "using Thrift.Processor;\n";
}
return namespaces; return namespaces;
} }

View File

@ -65,6 +65,7 @@ public:
bool is_hashcode_enabled() const; bool is_hashcode_enabled() const;
bool is_serialize_enabled() const; bool is_serialize_enabled() const;
bool is_union_enabled() const; bool is_union_enabled() const;
bool is_unity_enabled() const;
// overrides // overrides
void init_generator() override; void init_generator() override;
@ -169,6 +170,7 @@ private:
string namespace_name_; string namespace_name_;
string namespace_dir_; string namespace_dir_;
bool unity_;
bool union_; bool union_;
bool hashcode_; bool hashcode_;
bool serialize_; bool serialize_;

Binary file not shown.

Binary file not shown.

View File

@ -1,5 +1,6 @@
@echo off @echo off
if not exist generated md generated if not exist generated md generated
if not exist generated\meowment md generated\meowment if exist generated\meowment rd /s /q generated\meowment
for %%i in (thrift-files/meowment/*.thrift) do .\compiler\exe\thrift.exe -o .\generated\meowment -strict -v -r --gen netstd:serial,async_postfix .\thrift-files\meowment\%%i md generated\meowment
for %%i in (thrift-files/meowment/*.thrift) do .\compiler\exe\thrift.exe -o .\generated\meowment -strict -v -r --gen netstd:unity,serial,async_postfix .\thrift-files\meowment\%%i
pause pause

View File

@ -140,6 +140,7 @@ Available generators (and options):
wcf: Adds bindings for WCF to generated classes. wcf: Adds bindings for WCF to generated classes.
serial: Add serialization support to generated classes. serial: Add serialization support to generated classes.
union: Use new union typing, which includes a static read function for union types. union: Use new union typing, which includes a static read function for union types.
unity: Generate code for Unity.
pascal: Generate Pascal Case property names according to Microsoft naming convention. pascal: Generate Pascal Case property names according to Microsoft naming convention.
net8: Enable features that require net8 and C# 12 or higher. net8: Enable features that require net8 and C# 12 or higher.
net9: Enable features that require net9 and C# 13 or higher. net9: Enable features that require net9 and C# 13 or higher.