<commit_before>#!/usr/bin/ruby1.9.1

def create_interface line, func
	line.sub("public ", "")
end

def create_lib line, func
	<<-EOD
#{line}
{
	return _ExtensionContext.call("#{func[:air_name]}"#{["", *func[:args].keys].join(", ")}) as #{func[:ret]};
}
	EOD
end

@lib_lin_defaults = Hash.new { |h,k| h[k] = ["readResponse() as #{k}", "null"] }
@lib_lin_defaults.merge!({
	"Boolean" => ["readBoolResponse()", "false"],
	"Number" => ["readFloatResponse()", "0.0"],
	"int" => ["readIntResponse()", "0"],
	"uint" => ["readIntResponse()", "0"],
	"String" => ["readStringResponse()", "\"\""]
})
def create_lib_linux line, func
	type, default = @lib_lin_defaults[func[:ret]]

	<<-EOD
#{line} {
	if(!callWrapper(#{func[:air_name]}, [#{func[:args].keys.join(", ")}])) return #{default};
	return #{type};
}
	EOD
end

def create_constants_linux line, func
	"private static const #{func[:air_name]}:int = #{func[:num]};"
end

def create_macro line, func
	"X(#{func[:air_name]}) /* = #{func[:num]} */"
end

files = [
	{
		:file => "src/com/amanitadesign/steam/ISteamWorks.as",
		:ignore => ["init"],
		:format => method(:create_interface)
	},
	{
		:file => "src/com/amanitadesign/steam/FRESteamWorks.as",
		:ignore => ["init"],
		:format => method(:create_lib)
	},
	{
		:file => "src_linux/com/amanitadesign/steam/FRESteamWorks.as",
		:ignore => ["init", "runCallbacks", "useCrashHandler", "fileRead", "UGCRead"],
		:format => method(:create_lib_linux)
	},
	{
		:file => "src_linux/com/amanitadesign/steam/FRESteamWorks.as",
		:ignore => [],
		:format => method(:create_constants_linux),
		:start => "START GENERATED VALUES",
		:end => "END GENERATED VALUES"
	},
	{
		:file => "../src/FRESteamWorks/functions.h",
		:ignore => [],
		:format => method(:create_macro)
	}
]

def parse_prototype line
	match = line.match /function ([^(]+)\(([^)]*)\):(\w+)$/
	raise "Invalid line: #{line}" unless match

	func, args, ret = match.captures
	return {
		:name => func,
		:air_name => "AIRSteam_#{func[0].chr.upcase + func[1,func.size]}",
		# Ruby 1.9 guarantees that insertion order is maintained
		:args => Hash[args.scan(/(\w+):([^,]+)(?:,\s*)?/)],
		:ret => ret
	}
end

def generate_api contents, files
	files.each do |options|
		file_content = File.read(options[:file]).split("\n")<commit_msg>Ensure hash insertion order is maintained.<commit_after>#!/usr/bin/ruby1.9.1

def create_interface line, func
	line.sub("public ", "")
end

def create_lib line, func
	<<-EOD
#{line}
{
	return _ExtensionContext.call("#{func[:air_name]}"#{["", *func[:args].keys].join(", ")}) as #{func[:ret]};
}
	EOD
end

@lib_lin_defaults = Hash.new { |h,k| h[k] = ["readResponse() as #{k}", "null"] }
@lib_lin_defaults.merge!({
	"Boolean" => ["readBoolResponse()", "false"],
	"Number" => ["readFloatResponse()", "0.0"],
	"int" => ["readIntResponse()", "0"],
	"uint" => ["readIntResponse()", "0"],
	"String" => ["readStringResponse()", "\"\""]
})
def create_lib_linux line, func
	type, default = @lib_lin_defaults[func[:ret]]

	<<-EOD
#{line} {
	if(!callWrapper(#{func[:air_name]}, [#{func[:args].keys.join(", ")}])) return #{default};
	return #{type};
}
	EOD
end

def create_constants_linux line, func
	"private static const #{func[:air_name]}:int = #{func[:num]};"
end

def create_macro line, func
	"X(#{func[:air_name]}) /* = #{func[:num]} */"
end

files = [
	{
		:file => "src/com/amanitadesign/steam/ISteamWorks.as",
		:ignore => ["init"],
		:format => method(:create_interface)
	},
	{
		:file => "src/com/amanitadesign/steam/FRESteamWorks.as",
		:ignore => ["init"],
		:format => method(:create_lib)
	},
	{
		:file => "src_linux/com/amanitadesign/steam/FRESteamWorks.as",
		:ignore => ["init", "runCallbacks", "useCrashHandler", "fileRead", "UGCRead"],
		:format => method(:create_lib_linux)
	},
	{
		:file => "src_linux/com/amanitadesign/steam/FRESteamWorks.as",
		:ignore => [],
		:format => method(:create_constants_linux),
		:start => "START GENERATED VALUES",
		:end => "END GENERATED VALUES"
	},
	{
		:file => "../src/FRESteamWorks/functions.h",
		:ignore => [],
		:format => method(:create_macro)
	}
]

def parse_prototype line
	match = line.match /function ([^(]+)\(([^)]*)\):(\w+)$/
	raise "Invalid line: #{line}" unless match

	func, args, ret = match.captures

	arg_spec = args.scan(/(\w+):([^,]+)(?:,\s*)?/)
	arg_map = Hash[arg_spec]
	raise RuntimeError.new("Hash insertion order not maintained") unless arg_spec == arg_map.to_a
	return {
		:name => func,
		:air_name => "AIRSteam_#{func[0].chr.upcase + func[1,func.size]}",
		# Ruby 1.9 guarantees that insertion order is maintained
		:args => arg_map,
		:ret => ret
	}
end

def generate_api contents, files
	files.each do |options|
		file_content = File.read(options[:file]).split("\n")
{'fraction_non_alphanumeric': 0.2310630171865054, 'fraction_numerical': 0.0046679397411415234, 'mean_word_length': 4.929559748427673, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 55, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1731886', 'n_tokens_mistral': 1883, 'n_tokens_neox': 1794, 'n_words': 343}