Skip to content

Commit

Permalink
Naive attempt to tackle #58
Browse files Browse the repository at this point in the history
  • Loading branch information
Dadoum committed Jun 20, 2024
1 parent 0a03361 commit 4e47513
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .github/actions/target-x86_64-windows-msvc/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ runs:
shell: bash
run: sudo apt-get install -y clang lld 7zip

- name: Set-up macOS cross-compilation
- name: Set-up Windows cross-compilation
shell: bash
run: |
mkdir -p $HOME/.ldc/
Expand Down
2 changes: 1 addition & 1 deletion source/sideload/package.d
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ void sideloadFull(
DeveloperSession developer,
Application app,
void delegate(double progress, string action) progressCallback,
bool isMultithreaded = true,
bool isMultithreaded = false,
) {
enum STEP_COUNT = 9.0;
auto log = getLogger();
Expand Down
17 changes: 10 additions & 7 deletions source/sideload/sign.d
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ Tuple!(PlistDict, PlistDict) sign(
auto log = getLogger();

auto bundleFolder = bundle.bundleDir;
auto fairPlayFolder = bundleFolder.buildPath("SC_Info");
enum fairPlayDir = "SC_Info";
auto fairPlayFolder = bundleFolder.buildPath(fairPlayDir);
if (file.exists(fairPlayFolder)) {
file.rmdirRecurse(fairPlayFolder);
}
Expand Down Expand Up @@ -85,7 +86,7 @@ Tuple!(PlistDict, PlistDict) sign(
const double stepSize = 1.0 / stepCount;

void signSubBundles() {
foreach (subBundle; parallel(subBundles)) {
foreach (subBundle; maybeParallel(subBundles, isMultithreaded)) {
auto bundleFiles = subBundle.sign(
identity,
provisioningProfiles,
Expand Down Expand Up @@ -129,7 +130,7 @@ Tuple!(PlistDict, PlistDict) sign(
scope MmFile memoryFile = new MmFile(subBundle.bundleDir.buildPath(subRelativePath));
ubyte[] fileData = cast(ubyte[]) memoryFile[];

foreach (hashCouple; parallel(hashPairs)) {
foreach (hashCouple; maybeParallel(hashPairs, isMultithreaded)) {
auto localHasher = hashCouple[0];
auto sha = hashCouple[1];
sha[] = localHasher.process(fileData)[];
Expand Down Expand Up @@ -178,7 +179,7 @@ Tuple!(PlistDict, PlistDict) sign(

// TODO re-use the original CodeResources if it already existed.
if (bundleFolder[$ - 1] == '/' || bundleFolder[$ - 1] == '\\') bundleFolder.length -= 1;
foreach(idx, absolutePath; parallel(bundleFiles)) {
foreach(idx, absolutePath; maybeParallel(bundleFiles, isMultithreaded)) {
// scope(exit) addProgress(fileStepSize);

string basename = baseName(absolutePath);
Expand All @@ -196,6 +197,8 @@ Tuple!(PlistDict, PlistDict) sign(
|| (relativePath.startsWith(frameworksDir) && relativePath[frameworksDir.length..$].toForwardSlashes().canFind('/'))
// if it's a file from a plugins folder, skip it as it is processed by some other thread.
|| (relativePath.startsWith(plugInsDir) && relativePath[plugInsDir.length..$].toForwardSlashes().canFind('/'))
// if it's a fairplay file, it should not exist anymore anyway.
|| (relativePath.startsWith(fairPlayDir))
) {
continue;
}
Expand All @@ -212,13 +215,13 @@ Tuple!(PlistDict, PlistDict) sign(
scope MmFile memoryFile = new MmFile(absolutePath);
ubyte[] fileData = cast(ubyte[]) memoryFile[];

foreach (hashCouple; parallel(hashPairs)) {
foreach (hashCouple; maybeParallel(hashPairs, isMultithreaded)) {
auto localHasher = hashCouple[0];
auto sha = hashCouple[1];
sha[] = localHasher.process(fileData)[];
}
} else {
foreach (hashCouple; parallel(hashPairs)) {
foreach (hashCouple; maybeParallel(hashPairs, isMultithreaded)) {
auto localHasher = hashCouple[0];
auto sha = hashCouple[1];
sha[] = localHasher.process(cast(ubyte[]) [])[];
Expand Down Expand Up @@ -273,7 +276,7 @@ Tuple!(PlistDict, PlistDict) sign(

double machOStepSize = stepSize / fatMachOs.length;

foreach (idx, fatMachOPair; parallel(fatMachOs)) {
foreach (idx, fatMachOPair; maybeParallel(fatMachOs, isMultithreaded)) {
scope(exit) addProgress(machOStepSize);
auto path = fatMachOPair.path;
auto fatMachO = fatMachOPair.machO;
Expand Down
53 changes: 53 additions & 0 deletions source/utils.d
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,56 @@ string toForwardSlashes(string s) {
return s;
}
}

auto maybeParallel(R)(R range, bool isMultithreaded) {
import std.parallelism;
import std.range.primitives;
struct RangeApplier {
R range;
this(R range) {
this.range = range;
}

int opApply(int delegate(size_t index, ElementType!R) dg) {
if (isMultithreaded) {
foreach (index, elem; range.parallel) {
int i = dg(index, elem);
if (i != 0) {
return i;
}
}
} else {
size_t index = 0;
foreach (ElementType!R elem; range) {
int i = dg(index, elem);
if (i != 0) {
return i;
}
index += 1;
}
}
return 0;
}

int opApply(int delegate(ElementType!R) dg) {
if (isMultithreaded) {
foreach (elem; range.parallel) {
int i = dg(elem);
if (i != 0) {
return i;
}
}
} else {
foreach (elem; range) {
int i = dg(elem);
if (i != 0) {
return i;
}
}
}
return 0;
}
}

return RangeApplier(range);
}

0 comments on commit 4e47513

Please sign in to comment.