Files

122 lines
8.3 KiB
Python

# coding: utf-8
from pathlib import Path
import hashlib,json,shutil
R=Path('/Users/korizatou114/Projects/とりにく工房ゲーム制作部');A=R/'artifacts/original-completion-2026-10-02';C=A/'p23-02-native-integer-window-codec-candidate-r285';P=A/'p23-02-native-pure-tag-projection-candidate-r274/source';assert not C.exists();assert shutil.disk_usage(A).free>=528*1048576
(C/'source').mkdir(parents=True);original=(P/'tree_numeric_core.cpp').read_text();text=original
helpers=r'''
// Godot ed1daf0bf's integer Array wire layout; checked against this engine
// once per core, never sample values. Every caller-owned Array stays intact.
bool integer_wire_checked=false;
bool integer_wire_compatible=false;
static void wire32(uint8_t *p,uint32_t value) {
for (unsigned i=0;i<4;++i) p[i]=uint8_t(value>>(8*i));
}
static void wire64(uint8_t *p,uint64_t value) {
for (unsigned i=0;i<8;++i) p[i]=uint8_t(value>>(8*i));
}
static int64_t integer_wire_width(int64_t value) {
return value<int64_t(INT32_MIN) || value>int64_t(INT32_MAX) ? 12 : 8;
}
static void wire_integer(uint8_t *p,int64_t value) {
const bool wide=integer_wire_width(value)==12;
wire32(p,uint32_t(Variant::INT) | (wide ? uint32_t(1)<<16 : 0));
if (wide) wire64(p+4,uint64_t(value)); else wire32(p+4,uint32_t(value));
}
static bool integer_wire_encode(const Array &values,PackedByteArray &out) {
const bool typed=values.is_typed();
if (typed && (values.get_typed_builtin()!=Variant::INT ||
!values.get_typed_class_name().is_empty() || values.get_typed_script().get_type()!=Variant::NIL)) return false;
const int64_t count=values.size(),header=typed ? 12 : 8;
// The engine encoder uses a signed-int byte length. Preserve fallback
// for oversized inputs and failed allocations, before owner mutation.
if (count<0 || count>(int64_t(INT32_MAX)-header)/12) return false;
std::vector<int64_t> integers;
integers.reserve(size_t(count));
int64_t size=header;
for (int64_t i=0;i<count;++i) {
const Variant value=values[i];
if (value.get_type()!=Variant::INT) return false;
const int64_t integer=value;
integers.push_back(integer);size+=integer_wire_width(integer);
}
if (out.resize(size)!=OK) return false;
uint8_t *p=out.ptrw();
wire32(p,uint32_t(Variant::ARRAY) | (typed ? uint32_t(1)<<16 : 0));
int64_t offset=4;
if (typed) {wire32(p+offset,uint32_t(Variant::INT));offset+=4;}
wire32(p+offset,uint32_t(count));offset+=4;
for (const int64_t value: integers) {wire_integer(p+offset,value);offset+=integer_wire_width(value);}
return offset==size;
}
bool integer_wire_ready() {
if (integer_wire_checked) return integer_wire_compatible;
Array empty,probe;
TypedArray<int64_t> typed_empty,typed_probe;
const int64_t boundaries[]={INT64_MIN,int64_t(INT32_MIN)-1,INT32_MIN,-1,0,1,INT32_MAX,int64_t(INT32_MAX)+1,INT64_MAX};
for (const int64_t value: boundaries) {probe.append(value);typed_probe.append(value);}
bool matches=true;
for (const Array &value: {empty,probe,Array(typed_empty),Array(typed_probe)}) {
PackedByteArray encoded;
matches=integer_wire_encode(value,encoded) && encoded==UtilityFunctions::var_to_bytes(value) && matches;
}
integer_wire_compatible=matches;integer_wire_checked=true;
return matches;
}
static bool integer_wire_update(const PackedByteArray &before,bool typed,int64_t old_count,
int64_t appended,bool pop_front,PackedByteArray &after) {
const int64_t header=typed ? 12 : 8;
if (before.size()<header || old_count<0) return false;
const bool remove_appended=pop_front && old_count==0;
int64_t skipped=0;
if (pop_front && old_count>0) {
if (before.size()<header+8) return false;
// Exact INT header was produced locally from the current values.
skipped=(before[header+2] & 1) ? 12 : 8;
if (before.size()<header+skipped) return false;
}
const int64_t added=remove_appended ? 0 : integer_wire_width(appended);
const int64_t size=before.size()-skipped+added;
if (size>INT32_MAX || after.resize(size)!=OK) return false;
uint8_t *p=after.ptrw();const uint8_t *q=before.ptr();
std::memcpy(p,q,size_t(header));
wire32(p+header-4,uint32_t(old_count+1-(pop_front ? 1 : 0)));
const int64_t kept=before.size()-header-skipped;
if (kept>0) std::memcpy(p+header,q+header+skipped,size_t(kept));
if (added>0) wire_integer(p+header+kept,appended);
return true;
}
'''
text=text.replace('#include <cmath>','#include <cmath>\n#include <cstdint>\n#include <cstring>',1)
needle=' std::map<I, std::vector<Edge>> cached_edges;';assert text.count(needle)==1;text=text.replace(needle,needle+helpers,1)
needle=' ClassDB::bind_method(D_METHOD("performance_record_sample", "owner", "metric_id", "duration_us"), &TheTreeNumericCore::performance_record_sample);';assert text.count(needle)==1
text=text.replace(needle,needle+'\n ClassDB::bind_method(D_METHOD("encode_integer_window", "values"), &TheTreeNumericCore::encode_integer_window);',1)
needle=' // Whole builtin integer record operation.'
method=''' Dictionary encode_integer_window(const Array &values) {
Dictionary result;const StringName supported("supported",false),bytes("bytes",false);
result[supported]=false;PackedByteArray encoded;
if (!integer_wire_ready() || !integer_wire_encode(values,encoded)) return result;
result[supported]=true;result[bytes]=encoded;return result;
}
'''
assert text.count(needle)==1;text=text.replace(needle,method+needle,1)
old=' for (int64_t i=0;i<values.size();++i) if (values[i].get_type()!=Variant::INT) return unsupported;';assert text.count(old)==1;text=text.replace(old,'',1)
old=' PackedByteArray before=UtilityFunctions::var_to_bytes(values);';assert text.count(old)==1
new=''' PackedByteArray before;
const bool fast_wire=integer_wire_ready() && integer_wire_encode(values,before);
if (!fast_wire) {
for (int64_t i=0;i<values.size();++i) if (values[i].get_type()!=Variant::INT) return unsupported;
before=UtilityFunctions::var_to_bytes(values);
}
const int64_t old_count=values.size();
''';text=text.replace(old,new.rstrip(),1)
old=' PackedByteArray after=UtilityFunctions::var_to_bytes(values);';assert text.count(old)==1
new=''' PackedByteArray after;
const bool popped=old_count+1>int64_t(capacityv);
if (!fast_wire || !integer_wire_update(before,values.is_typed(),old_count,duration,popped,after))
after=UtilityFunctions::var_to_bytes(values);''';text=text.replace(old,new,1)
(C/'source/tree_numeric_core.cpp').write_text(text);shutil.copyfile(P/'tree_core.hpp',C/'source/tree_core.hpp')
def pin(p):b=p.read_bytes();return dict(path=str(p),bytes=len(b),sha256=hashlib.sha256(b).hexdigest())
D=R/'docs/plans/original-completion-2026-10-02'
with (C/'input.json').open('x') as f:json.dump(dict(parent_cpp=pin(P/'tree_numeric_core.cpp'),parent_header=pin(P/'tree_core.hpp'),candidate_cpp=pin(C/'source/tree_numeric_core.cpp'),candidate_header=pin(C/'source/tree_core.hpp'),author=pin(Path(__file__)),primary_encoder=pin(A/'p23-02-integer-window-codec-source-r284/marshalls.cpp'),primary_encoder_reference=pin(A/'p23-02-integer-window-codec-source-r284/source.json'),budget=pin(A/'p23-02-native-budget-record-candidate-r257-r04/performance_budget.candidate.gd'),ledger_review=pin(A/'p23-02-progress-integration-actual-independent-review-r282.json'),doc4={n:pin(D/n) for n in ['IMPLEMENTATION_PLAN.md','PROGRESS_INTEGRATION.md','work_packages.json','progress_snapshot.json']},scope='source-only integer Array exact bytes encode and append/pop transformation; per-object engine format check, no cached sample values; old encoding fallback retained; report/values/thresholds/notifications/typed/ownership unchanged; performance and load unproven',engine_runs=0,compiler_runs=0,stage_clones=0,candidate_adopted=False,unit_accepted=False),f,ensure_ascii=False,indent=2)
print('source candidate prepared; source-only, no build or engine')