@@ -182,6 +182,23 @@ function contentdb.get_package_by_id(id)
182
182
end
183
183
184
184
185
+ function contentdb .calculate_package_id (type , author , name )
186
+ local id = author :lower () .. " /"
187
+ if (type == nil or type == " game" ) and # name > 5 and name :sub (# name - 4 ) == " _game" then
188
+ id = id .. name :sub (1 , # name - 5 )
189
+ else
190
+ id = id .. name
191
+ end
192
+ return id
193
+ end
194
+
195
+
196
+ function contentdb .get_package_by_info (author , name )
197
+ local id = contentdb .calculate_package_id (nil , author , name )
198
+ return contentdb .package_by_id [id ]
199
+ end
200
+
201
+
185
202
-- Create a coroutine from `fn` and provide results to `callback` when complete (dead).
186
203
-- Returns a resumer function.
187
204
local function make_callback_coroutine (fn , callback )
@@ -415,15 +432,7 @@ local function fetch_pkgs(params)
415
432
local aliases = {}
416
433
417
434
for _ , package in pairs (packages ) do
418
- local name_len = # package .name
419
- -- This must match what contentdb.update_paths() does!
420
- package .id = package .author :lower () .. " /"
421
- if package .type == " game" and name_len > 5 and package .name :sub (name_len - 4 ) == " _game" then
422
- package .id = package .id .. package .name :sub (1 , name_len - 5 )
423
- else
424
- package .id = package .id .. package .name
425
- end
426
-
435
+ package .id = params .calculate_package_id (package .type , package .author , package .name )
427
436
package .url_part = core .urlencode (package .author ) .. " /" .. core .urlencode (package .name )
428
437
429
438
if package .aliases then
443
452
444
453
function contentdb .fetch_pkgs (callback )
445
454
contentdb .loading = true
446
- core .handle_async (fetch_pkgs , nil , function (result )
455
+ core .handle_async (fetch_pkgs , { calculate_package_id = contentdb . calculate_package_id } , function (result )
447
456
if result then
448
457
contentdb .load_ok = true
449
458
contentdb .load_error = false
@@ -581,3 +590,78 @@ function contentdb.filter_packages(query, by_type)
581
590
end
582
591
end
583
592
end
593
+
594
+
595
+ function contentdb .get_full_package_info (package , callback )
596
+ assert (package )
597
+ if package .full_info then
598
+ callback (package .full_info )
599
+ return
600
+ end
601
+
602
+ local function fetch (params )
603
+ local version = core .get_version ()
604
+ local base_url = core .settings :get (" contentdb_url" )
605
+
606
+ local languages
607
+ local current_language = core .get_language ()
608
+ if current_language ~= " " then
609
+ languages = { current_language , " en;q=0.8" }
610
+ else
611
+ languages = { " en" }
612
+ end
613
+
614
+ local url = base_url ..
615
+ " /api/packages/" .. params .package .url_part .. " /for-client/?" ..
616
+ " protocol_version=" .. core .urlencode (core .get_max_supp_proto ()) ..
617
+ " &engine_version=" .. core .urlencode (version .string ) ..
618
+ " &formspec_version=" .. core .urlencode (core .get_formspec_version ()) ..
619
+ " &include_images=false"
620
+ local http = core .get_http_api ()
621
+ local response = http .fetch_sync ({
622
+ url = url ,
623
+ extra_headers = {
624
+ " Accept-Language: " .. table.concat (languages , " , " )
625
+ },
626
+ })
627
+ if not response .succeeded then
628
+ return nil
629
+ end
630
+
631
+ return core .parse_json (response .data )
632
+ end
633
+
634
+ local function my_callback (value )
635
+ package .full_info = value
636
+ callback (value )
637
+ end
638
+
639
+ if not core .handle_async (fetch , { package = package }, my_callback ) then
640
+ core .log (" error" , " ERROR: async event failed" )
641
+ callback (nil )
642
+ end
643
+ end
644
+
645
+
646
+ function contentdb .get_formspec_padding ()
647
+ -- Padding is increased on Android to account for notches
648
+ -- TODO: use Android API to determine size of cut outs
649
+ return { x = PLATFORM == " Android" and 1 or 0.5 , y = PLATFORM == " Android" and 0.25 or 0.5 }
650
+ end
651
+
652
+
653
+ function contentdb .get_formspec_size ()
654
+ local window = core .get_window_info ()
655
+ local size = { x = window .max_formspec_size .x , y = window .max_formspec_size .y }
656
+
657
+ -- Minimum formspec size
658
+ local min_x = 15.5
659
+ local min_y = 10
660
+ if size .x < min_x or size .y < min_y then
661
+ local scale = math.max (min_x / size .x , min_y / size .y )
662
+ size .x = size .x * scale
663
+ size .y = size .y * scale
664
+ end
665
+
666
+ return size
667
+ end
0 commit comments