|
| 1 | +require "option_parser" |
| 2 | +require "asar-cr" |
| 3 | +require "./version.cr" |
| 4 | +require "./functions/*" |
| 5 | +require "./locators/*" |
| 6 | +require "./plugins/core/*" |
| 7 | +require "./plugins/extra/*" |
| 8 | + |
| 9 | +module Crycord |
| 10 | + extend self |
| 11 | + @@plugins : Array(String) |
| 12 | + @@available_groups : Array(String) |
| 13 | + |
| 14 | + @@plugins = Crycord::PLUGINS.keys.reject! { |x| Crycord::PLUGINS[x].disabled } |
| 15 | + @@available_groups = @@plugins.map { |x| x = Crycord::PLUGINS[x].category }.uniq |
| 16 | + css_path = "" |
| 17 | + asar_path = "" |
| 18 | + groups = ["core"] |
| 19 | + should_revert = false |
| 20 | + |
| 21 | + def get_paths : String |
| 22 | + path = flatpak |
| 23 | + if path.nil? |
| 24 | + path = linux |
| 25 | + else |
| 26 | + puts "Flatpak Detected:" |
| 27 | + puts "Make sure it has access to your CSS file" |
| 28 | + puts "Usually ~/Downloads is accessible" |
| 29 | + end |
| 30 | + if path.nil? |
| 31 | + puts "ERROR: couldn't find core.asar, try manually setting it using the -f flag." |
| 32 | + exit |
| 33 | + end |
| 34 | + return path.to_s |
| 35 | + end |
| 36 | + |
| 37 | + def group_list |
| 38 | + puts "Available plugin groups:" |
| 39 | + puts @@available_groups.join("\n") |
| 40 | + puts "Note: core is being installed by default" |
| 41 | + end |
| 42 | + |
| 43 | + # CLI options |
| 44 | + OptionParser.parse do |parser| |
| 45 | + parser.banner = "<== [Crycord] ==>" |
| 46 | + |
| 47 | + parser.on "-v", "--version", "Show version" do |
| 48 | + puts "Crycord" |
| 49 | + puts "Version: #{VERSION}" |
| 50 | + exit |
| 51 | + end |
| 52 | + parser.on "-h", "--help", "Show help" do |
| 53 | + puts parser |
| 54 | + exit |
| 55 | + end |
| 56 | + parser.on "-gs", "--groups", "Lists all available plugin groups" do |
| 57 | + group_list |
| 58 | + exit |
| 59 | + end |
| 60 | + parser.on "-r", "--revert", "Reverts back to original asar" do |
| 61 | + should_revert = true |
| 62 | + end |
| 63 | + parser.on "-p", "--plugins", "Lists all available plugins" do |
| 64 | + available_plugins = Crycord::PLUGINS.keys.map { |x| x = Crycord::PLUGINS[x].name + " | " + Crycord::PLUGINS[x].category + " | " + Crycord::PLUGINS[x].desc }.uniq |
| 65 | + puts "Available plugins:" |
| 66 | + puts "NAME | GROUP | DESCRIPTION" |
| 67 | + puts available_plugins.reject! { |x| Crycord::PLUGINS[x].disabled }.join("\n") |
| 68 | + puts "Note: Disabled plugins are omitted" |
| 69 | + exit |
| 70 | + end |
| 71 | + parser.on "-c CSS_PATH", "--css=CSS_PATH", "Sets CSS location" do |path| |
| 72 | + css = Path[path].expand(home: true) |
| 73 | + unless File.exists?(css) |
| 74 | + puts "ERROR: CSS file not found" |
| 75 | + exit |
| 76 | + end |
| 77 | + css_path = css.to_s |
| 78 | + end |
| 79 | + parser.on "-f CORE_ASAR_PATH", "--force=CORE_ASAR_PATH", "Forces an asar path" do |path| |
| 80 | + asar_path = Path[path].expand(home: true).to_s |
| 81 | + unless File.exists?(asar_path) |
| 82 | + puts "ERROR: core.asar not found" |
| 83 | + exit |
| 84 | + end |
| 85 | + end |
| 86 | + parser.on "-g PLUGIN_GROUP", "--group=PLUGIN_GROUP", "Selects the plugin group(s) to install. Split multiple groups with commas(,)." do |input| |
| 87 | + if input == "" || input.nil? |
| 88 | + group_list |
| 89 | + end |
| 90 | + groups.concat(input.downcase.gsub(" ", "").split(",")) |
| 91 | + groups.uniq! |
| 92 | + groups.each do |item| |
| 93 | + unless @@available_groups.includes?(item) |
| 94 | + puts "ERROR: unknown group, use the -gs flag to list all groups." |
| 95 | + exit |
| 96 | + end |
| 97 | + end |
| 98 | + end |
| 99 | + |
| 100 | + parser.missing_option do |option_flag| |
| 101 | + STDERR.puts "ERROR: #{option_flag} is missing something." |
| 102 | + STDERR.puts "" |
| 103 | + STDERR.puts parser |
| 104 | + exit(1) |
| 105 | + end |
| 106 | + parser.invalid_option do |option_flag| |
| 107 | + STDERR.puts "ERROR: #{option_flag} is not a valid option." |
| 108 | + STDERR.puts parser |
| 109 | + exit(1) |
| 110 | + end |
| 111 | + end |
| 112 | + |
| 113 | + # Check revert |
| 114 | + if should_revert |
| 115 | + asar_path = get_paths if asar_path == "" |
| 116 | + res = revert(Path[asar_path]) |
| 117 | + puts "Restore was #{res.nil? ? "un" : ""}successful" |
| 118 | + exit |
| 119 | + end |
| 120 | + |
| 121 | + # Check options |
| 122 | + if css_path == "" |
| 123 | + puts "ERROR: -c option is missing" |
| 124 | + exit |
| 125 | + end |
| 126 | + |
| 127 | + # Check discord and get paths |
| 128 | + asar_path = get_paths if asar_path == "" |
| 129 | + |
| 130 | + # Extract asar |
| 131 | + puts "Extracting core.asar..." |
| 132 | + path = extract(Path[asar_path]) |
| 133 | + if path.nil? |
| 134 | + puts "ERROR: couldn't extract core.asar" |
| 135 | + exit |
| 136 | + end |
| 137 | + |
| 138 | + # See collector.cr |
| 139 | + modules = Crycord::Plugins.collected_modules |
| 140 | + selected_plugins = @@plugins.reject! { |x| !groups.includes?(Crycord::PLUGINS[x].category) } |
| 141 | + |
| 142 | + selected_plugins.each do |plugin| |
| 143 | + plugin_module = modules.find { |i| i.to_s == "Crycord::Plugins::#{plugin.upcase}" } |
| 144 | + css = Crycord::PLUGINS[plugin].css ? css_path : nil |
| 145 | + puts "Installing #{plugin}..." |
| 146 | + plugin_module.try &.execute(path, css) |
| 147 | + end |
| 148 | + |
| 149 | + puts "Packing core.asar..." |
| 150 | + pack(Path[path]) |
| 151 | + puts "Done!" |
| 152 | + puts "Restart Discord to see the results!" |
| 153 | +end |
0 commit comments