-
Notifications
You must be signed in to change notification settings - Fork 14
[WIP] Support arbitrary .bin map filenames instead of map_XXX.bin convention #244
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: 3.0
Are you sure you want to change the base?
Conversation
…0.bin 🤖 Generated with [Claude Code](https://claude.com/claude-code)
Greptile SummaryRemoved the Key changes:
Issues found:
Confidence Score: 3/5
Important Files Changed
Sequence DiagramsequenceDiagram
participant User
participant Python as drive.py
participant C as binding.c
participant FS as File System
User->>Python: Initialize Drive environment
Python->>Python: Check map_dir exists
Python->>FS: os.listdir(map_dir)
FS-->>Python: List of files
Python->>Python: Filter .bin files
alt No .bin files found
Python-->>User: FileNotFoundError
end
Python->>C: Call my_shared() or my_init()
C->>C: scan_map_files(map_dir)
C->>FS: opendir(map_dir)
FS-->>C: Directory handle
loop First pass
C->>FS: readdir()
FS-->>C: Directory entry
C->>C: Count .bin files
end
C->>C: Allocate filenames array
C->>FS: rewinddir()
loop Second pass
C->>FS: readdir()
FS-->>C: Directory entry
C->>C: Build full path, store in array
end
C->>C: qsort(filenames) for deterministic ordering
C->>FS: closedir()
alt use_all_maps mode
loop For each map file
C->>C: Load map by index
C->>C: Check for controllable agents
end
else random sampling mode
loop Until num_agents met
C->>C: Select random map_id
C->>C: Load map_files.filenames[map_id]
C->>C: Check for controllable agents
end
end
C->>C: free_map_file_list()
C-->>Python: Environment initialized
Python-->>User: Ready for training
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
6 files reviewed, 5 comments
pufferlib/ocean/drive/binding.c
Outdated
| } | ||
|
|
||
| // Allocate array | ||
| result.filenames = malloc(count * sizeof(char *)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
logic: Missing null check after malloc. If allocation fails, subsequent code will crash.
| result.filenames = malloc(count * sizeof(char *)); | |
| result.filenames = malloc(count * sizeof(char *)); | |
| if (!result.filenames) { | |
| closedir(dir); | |
| return result; | |
| } |
Prompt To Fix With AI
This is a comment left during a code review.
Path: pufferlib/ocean/drive/binding.c
Line: 44:44
Comment:
**logic:** Missing null check after `malloc`. If allocation fails, subsequent code will crash.
```suggestion
result.filenames = malloc(count * sizeof(char *));
if (!result.filenames) {
closedir(dir);
return result;
}
```
How can I resolve this? If you propose a fix, please make it concise.
pufferlib/ocean/drive/binding.c
Outdated
| if (len > 4 && strcmp(name + len - 4, ".bin") == 0) { | ||
| // Allocate full path | ||
| size_t path_len = strlen(map_dir) + 1 + len + 1; | ||
| result.filenames[idx] = malloc(path_len); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
logic: Missing null check after malloc. If allocation fails for individual filename, will cause crash when accessed.
| result.filenames[idx] = malloc(path_len); | |
| result.filenames[idx] = malloc(path_len); | |
| if (!result.filenames[idx]) { | |
| // Free previously allocated strings | |
| for (int i = 0; i < idx; i++) { | |
| free(result.filenames[i]); | |
| } | |
| free(result.filenames); | |
| closedir(dir); | |
| result.filenames = NULL; | |
| result.count = 0; | |
| return result; | |
| } |
Prompt To Fix With AI
This is a comment left during a code review.
Path: pufferlib/ocean/drive/binding.c
Line: 56:56
Comment:
**logic:** Missing null check after `malloc`. If allocation fails for individual filename, will cause crash when accessed.
```suggestion
result.filenames[idx] = malloc(path_len);
if (!result.filenames[idx]) {
// Free previously allocated strings
for (int i = 0; i < idx; i++) {
free(result.filenames[i]);
}
free(result.filenames);
closedir(dir);
result.filenames = NULL;
result.count = 0;
return result;
}
```
How can I resolve this? If you propose a fix, please make it concise.
pufferlib/ocean/drive/visualize.c
Outdated
| } | ||
|
|
||
| // Allocate array | ||
| result.filenames = malloc(count * sizeof(char *)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
logic: Missing null check after malloc. Same issue as in binding.c.
| result.filenames = malloc(count * sizeof(char *)); | |
| result.filenames = malloc(count * sizeof(char *)); | |
| if (!result.filenames) { | |
| closedir(dir); | |
| return result; | |
| } |
Prompt To Fix With AI
This is a comment left during a code review.
Path: pufferlib/ocean/drive/visualize.c
Line: 55:55
Comment:
**logic:** Missing null check after `malloc`. Same issue as in `binding.c`.
```suggestion
result.filenames = malloc(count * sizeof(char *));
if (!result.filenames) {
closedir(dir);
return result;
}
```
How can I resolve this? If you propose a fix, please make it concise.
pufferlib/ocean/drive/visualize.c
Outdated
| if (len > 4 && strcmp(name + len - 4, ".bin") == 0) { | ||
| // Allocate full path | ||
| size_t path_len = strlen(map_dir) + 1 + len + 1; | ||
| result.filenames[idx] = malloc(path_len); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
logic: Missing null check after malloc for individual filename allocation.
| result.filenames[idx] = malloc(path_len); | |
| result.filenames[idx] = malloc(path_len); | |
| if (!result.filenames[idx]) { | |
| for (int i = 0; i < idx; i++) { | |
| free(result.filenames[i]); | |
| } | |
| free(result.filenames); | |
| closedir(dir); | |
| result.filenames = NULL; | |
| result.count = 0; | |
| return result; | |
| } |
Prompt To Fix With AI
This is a comment left during a code review.
Path: pufferlib/ocean/drive/visualize.c
Line: 67:67
Comment:
**logic:** Missing null check after `malloc` for individual filename allocation.
```suggestion
result.filenames[idx] = malloc(path_len);
if (!result.filenames[idx]) {
for (int i = 0; i < idx; i++) {
free(result.filenames[i]);
}
free(result.filenames);
closedir(dir);
result.filenames = NULL;
result.count = 0;
return result;
}
```
How can I resolve this? If you propose a fix, please make it concise.
pufferlib/ocean/drive/visualize.c
Outdated
| // Structure to hold discovered map files | ||
| typedef struct { | ||
| char **filenames; | ||
| int count; | ||
| } MapFileList; | ||
|
|
||
| // Compare function for qsort to sort filenames alphabetically | ||
| static int compare_strings(const void *a, const void *b) { | ||
| return strcmp(*(const char **)a, *(const char **)b); | ||
| } | ||
|
|
||
| // Scan directory for .bin files and return sorted list | ||
| static MapFileList scan_map_files(const char *map_dir) { | ||
| MapFileList result = {NULL, 0}; | ||
| DIR *dir = opendir(map_dir); | ||
| if (!dir) { | ||
| return result; | ||
| } | ||
|
|
||
| // First pass: count .bin files | ||
| struct dirent *entry; | ||
| int count = 0; | ||
| while ((entry = readdir(dir)) != NULL) { | ||
| const char *name = entry->d_name; | ||
| size_t len = strlen(name); | ||
| if (len > 4 && strcmp(name + len - 4, ".bin") == 0) { | ||
| count++; | ||
| } | ||
| } | ||
|
|
||
| if (count == 0) { | ||
| closedir(dir); | ||
| return result; | ||
| } | ||
|
|
||
| // Allocate array | ||
| result.filenames = malloc(count * sizeof(char *)); | ||
| result.count = count; | ||
|
|
||
| // Second pass: collect filenames | ||
| rewinddir(dir); | ||
| int idx = 0; | ||
| while ((entry = readdir(dir)) != NULL && idx < count) { | ||
| const char *name = entry->d_name; | ||
| size_t len = strlen(name); | ||
| if (len > 4 && strcmp(name + len - 4, ".bin") == 0) { | ||
| // Allocate full path | ||
| size_t path_len = strlen(map_dir) + 1 + len + 1; | ||
| result.filenames[idx] = malloc(path_len); | ||
| snprintf(result.filenames[idx], path_len, "%s/%s", map_dir, name); | ||
| idx++; | ||
| } | ||
| } | ||
| closedir(dir); | ||
|
|
||
| // Sort for deterministic ordering | ||
| qsort(result.filenames, result.count, sizeof(char *), compare_strings); | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
| static void free_map_file_list(MapFileList *list) { | ||
| if (list->filenames) { | ||
| for (int i = 0; i < list->count; i++) { | ||
| free(list->filenames[i]); | ||
| } | ||
| free(list->filenames); | ||
| list->filenames = NULL; | ||
| list->count = 0; | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
style: The scan_map_files, compare_strings, and free_map_file_list functions are duplicated from binding.c
Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!
Prompt To Fix With AI
This is a comment left during a code review.
Path: pufferlib/ocean/drive/visualize.c
Line: 19:89
Comment:
**style:** The `scan_map_files`, `compare_strings`, and `free_map_file_list` functions are duplicated from `binding.c`
<sub>Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!</sub>
How can I resolve this? If you propose a fix, please make it concise.6885336 to
9e5c0c9
Compare
- Add directory scanning utility to discover .bin files dynamically - Update binding.c to scan map directory and load maps by index into sorted list - Update visualize.c to pick random maps from scanned directory - Update pufferl.py sanity check to use per-map subdirectories - Add comprehensive test suite for map filename flexibility 🤖 Generated with [Claude Code](https://claude.com/claude-code)
9e5c0c9 to
4a78e45
Compare
🤖 Generated with [Claude Code](https://claude.com/claude-code)
…fix/flexible_map_names
Python already validates num_maps against available maps with proper user feedback (error or warning). The C-side check was redundant. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
- Python builds sorted map_files list once, passes to C binding - C no longer scans directory independently (removes race condition risk) - my_shared() accepts map_files list instead of map_dir - my_init() accepts map_path instead of map_dir + map_id - Fix memory leaks in visualize.c error paths - Rename allow_map_resampling -> allow_fewer_maps for clarity 🤖 Generated with [Claude Code](https://claude.com/claude-code)
🤖 Generated with [Claude Code](https://claude.com/claude-code)
🤖 Generated with [Claude Code](https://claude.com/claude-code)
|
@l1onh3art88 @daphne-cornelisse reason for doing this is so we can keep the CARLA maps with the same names |
|
Fine with me, but feels like a lot of added code for a relatively arbitrary change. Might be missing something |
Missed this. So you want the files to be e.g., womd_0000, womd_0001, carla_000, etc. ? That makes sense |
|
oh no they can be anything now! town_01.bin, highway.bin, whatever |
|
I was annoyed that I was naming carla towns map_001.bin and then not being sure what it actually was once I had forgotten the mapping |
|
If you remove the tests (optional) and the changes to visualize.c (also optional) it's not a lot of code |
|
@daphne-cornelisse point taken, I've removed quite a bit of code to make this more compact |
468439a to
f26c600
Compare
- Revert visualize.c directory scanning (use 2.0 base) - Python now picks random map from env_cfg.map_dir - Ensures --env.map-dir propagates to visualizer via --map-name 🤖 Generated with [Claude Code](https://claude.com/claude-code)
f26c600 to
44244a0
Compare
Nice! much better |
Summary
Builds on #243 to fully remove the
map_XXX.binnaming requirement..binfiles dynamically in Chighway.bin,downtown.bin) instead of requiringmap_000.bin,map_001.bin, etc.Test plan
All tests passing:
map_000.bin,map_001.bin(backward compatible).binfiles🤖 Generated with Claude Code