Skip to content
47 changes: 46 additions & 1 deletion vrchat_dart/tool/patch_output.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,21 @@ import 'package:fixer/fixer.dart';
import 'package:path/path.dart' as path;
import 'package:recase/recase.dart';

const defaultFormData = "FormData.fromMap({'file': file});";
const formDataPatches = <String, Map<String, String>>{
'files_api.dart': {
'/file/image': '''
FormData.fromMap({
'file': file,
'tag': tag,
if (animationStyle != null) 'animationStyle': animationStyle,
if (maskTag != null) 'maskTag': maskTag,
});''',
'/icon': defaultFormData,
'/gallery': defaultFormData,
},
};

void main() {
final spec =
jsonDecode(File(path.join('build', 'spec.json')).readAsStringSync());
Expand Down Expand Up @@ -95,7 +110,20 @@ void patchApi() {
.whereType<File>();

for (final file in apiFiles) {
final content = file.readAsStringSync();
var content = file.readAsStringSync();

final fdp =
formDataPatches[file.path.split(Platform.pathSeparator).last] ?? {};
final formDatas = RegExp(r'multipart\/form-data').allMatches(content);

if (fdp.length != formDatas.length) {
throw Exception('Missing FormData patches for ${file.path}');
}

for (final MapEntry(key: path, value: bodyData) in fdp.entries) {
content = content.patchMultipartUpload(path: path, bodyData: bodyData);
}

file.writeAsStringSync(content);
}
}
Expand Down Expand Up @@ -157,3 +185,20 @@ void patchAnalysisIssues() {
file.writeAsStringSync(contents);
}
}

extension on String {
String patchMultipartUpload({
required String path,
required String bodyData,
}) {
final escapedPath = RegExp.escape(path);
final tabbedBodyData = bodyData.replaceAll('\n', '\n ');
return replaceFirstMapped(
RegExp("final _path = r'$escapedPath';(.+?)try {}", dotAll: true),
(m) => '''
final _path = r'$path';${m[1]}try {
_bodyData = $tabbedBodyData
}''',
);
}
}
17 changes: 14 additions & 3 deletions vrchat_dart_generated/lib/src/api/files_api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1212,7 +1212,9 @@ class FilesApi {

dynamic _bodyData;

try {} catch (error, stackTrace) {
try {
_bodyData = FormData.fromMap({'file': file});
} catch (error, stackTrace) {
throw DioException(
requestOptions: _options.compose(
_dio.options,
Expand Down Expand Up @@ -1308,7 +1310,9 @@ class FilesApi {

dynamic _bodyData;

try {} catch (error, stackTrace) {
try {
_bodyData = FormData.fromMap({'file': file});
} catch (error, stackTrace) {
throw DioException(
requestOptions: _options.compose(
_dio.options,
Expand Down Expand Up @@ -1410,7 +1414,14 @@ class FilesApi {

dynamic _bodyData;

try {} catch (error, stackTrace) {
try {
_bodyData = FormData.fromMap({
'file': file,
'tag': tag,
if (animationStyle != null) 'animationStyle': animationStyle,
if (maskTag != null) 'maskTag': maskTag,
});
} catch (error, stackTrace) {
throw DioException(
requestOptions: _options.compose(
_dio.options,
Expand Down